@aztec/archiver 0.0.0-test.0 → 0.0.1-commit.21caa21

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 (110) hide show
  1. package/README.md +27 -6
  2. package/dest/archiver/archiver.d.ts +147 -57
  3. package/dest/archiver/archiver.d.ts.map +1 -1
  4. package/dest/archiver/archiver.js +841 -333
  5. package/dest/archiver/archiver_store.d.ts +85 -50
  6. package/dest/archiver/archiver_store.d.ts.map +1 -1
  7. package/dest/archiver/archiver_store_test_suite.d.ts +1 -1
  8. package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
  9. package/dest/archiver/archiver_store_test_suite.js +708 -213
  10. package/dest/archiver/config.d.ts +5 -21
  11. package/dest/archiver/config.d.ts.map +1 -1
  12. package/dest/archiver/config.js +21 -12
  13. package/dest/archiver/data_retrieval.d.ts +32 -27
  14. package/dest/archiver/data_retrieval.d.ts.map +1 -1
  15. package/dest/archiver/data_retrieval.js +197 -94
  16. package/dest/archiver/errors.d.ts +9 -1
  17. package/dest/archiver/errors.d.ts.map +1 -1
  18. package/dest/archiver/errors.js +12 -0
  19. package/dest/archiver/index.d.ts +3 -4
  20. package/dest/archiver/index.d.ts.map +1 -1
  21. package/dest/archiver/index.js +1 -2
  22. package/dest/archiver/instrumentation.d.ts +12 -6
  23. package/dest/archiver/instrumentation.d.ts.map +1 -1
  24. package/dest/archiver/instrumentation.js +58 -17
  25. package/dest/archiver/kv_archiver_store/block_store.d.ts +48 -11
  26. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
  27. package/dest/archiver/kv_archiver_store/block_store.js +216 -63
  28. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +3 -3
  29. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -1
  30. package/dest/archiver/kv_archiver_store/contract_class_store.js +12 -18
  31. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +11 -8
  32. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
  33. package/dest/archiver/kv_archiver_store/contract_instance_store.js +30 -16
  34. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +50 -35
  35. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
  36. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +88 -46
  37. package/dest/archiver/kv_archiver_store/log_store.d.ts +2 -2
  38. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
  39. package/dest/archiver/kv_archiver_store/log_store.js +18 -46
  40. package/dest/archiver/kv_archiver_store/message_store.d.ts +23 -17
  41. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
  42. package/dest/archiver/kv_archiver_store/message_store.js +150 -48
  43. package/dest/archiver/structs/data_retrieval.d.ts +1 -1
  44. package/dest/archiver/structs/inbox_message.d.ts +15 -0
  45. package/dest/archiver/structs/inbox_message.d.ts.map +1 -0
  46. package/dest/archiver/structs/inbox_message.js +38 -0
  47. package/dest/archiver/structs/published.d.ts +3 -11
  48. package/dest/archiver/structs/published.d.ts.map +1 -1
  49. package/dest/archiver/structs/published.js +1 -1
  50. package/dest/archiver/validation.d.ts +17 -0
  51. package/dest/archiver/validation.d.ts.map +1 -0
  52. package/dest/archiver/validation.js +98 -0
  53. package/dest/factory.d.ts +8 -13
  54. package/dest/factory.d.ts.map +1 -1
  55. package/dest/factory.js +18 -49
  56. package/dest/index.d.ts +2 -2
  57. package/dest/index.d.ts.map +1 -1
  58. package/dest/index.js +1 -1
  59. package/dest/rpc/index.d.ts +2 -3
  60. package/dest/rpc/index.d.ts.map +1 -1
  61. package/dest/rpc/index.js +1 -4
  62. package/dest/test/index.d.ts +1 -1
  63. package/dest/test/mock_archiver.d.ts +2 -2
  64. package/dest/test/mock_archiver.d.ts.map +1 -1
  65. package/dest/test/mock_l1_to_l2_message_source.d.ts +5 -3
  66. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  67. package/dest/test/mock_l1_to_l2_message_source.js +14 -1
  68. package/dest/test/mock_l2_block_source.d.ts +38 -10
  69. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  70. package/dest/test/mock_l2_block_source.js +119 -8
  71. package/dest/test/mock_structs.d.ts +9 -0
  72. package/dest/test/mock_structs.d.ts.map +1 -0
  73. package/dest/test/mock_structs.js +37 -0
  74. package/package.json +28 -30
  75. package/src/archiver/archiver.ts +1087 -410
  76. package/src/archiver/archiver_store.ts +97 -55
  77. package/src/archiver/archiver_store_test_suite.ts +664 -210
  78. package/src/archiver/config.ts +28 -41
  79. package/src/archiver/data_retrieval.ts +279 -125
  80. package/src/archiver/errors.ts +21 -0
  81. package/src/archiver/index.ts +2 -3
  82. package/src/archiver/instrumentation.ts +77 -22
  83. package/src/archiver/kv_archiver_store/block_store.ts +270 -72
  84. package/src/archiver/kv_archiver_store/contract_class_store.ts +13 -23
  85. package/src/archiver/kv_archiver_store/contract_instance_store.ts +35 -27
  86. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +127 -63
  87. package/src/archiver/kv_archiver_store/log_store.ts +24 -62
  88. package/src/archiver/kv_archiver_store/message_store.ts +209 -53
  89. package/src/archiver/structs/inbox_message.ts +41 -0
  90. package/src/archiver/structs/published.ts +2 -11
  91. package/src/archiver/validation.ts +124 -0
  92. package/src/factory.ts +24 -66
  93. package/src/index.ts +1 -1
  94. package/src/rpc/index.ts +1 -5
  95. package/src/test/mock_archiver.ts +1 -1
  96. package/src/test/mock_l1_to_l2_message_source.ts +14 -3
  97. package/src/test/mock_l2_block_source.ts +158 -13
  98. package/src/test/mock_structs.ts +49 -0
  99. package/dest/archiver/kv_archiver_store/nullifier_store.d.ts +0 -12
  100. package/dest/archiver/kv_archiver_store/nullifier_store.d.ts.map +0 -1
  101. package/dest/archiver/kv_archiver_store/nullifier_store.js +0 -73
  102. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts +0 -23
  103. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts.map +0 -1
  104. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.js +0 -49
  105. package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts +0 -175
  106. package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts.map +0 -1
  107. package/dest/archiver/memory_archiver_store/memory_archiver_store.js +0 -636
  108. package/src/archiver/kv_archiver_store/nullifier_store.ts +0 -97
  109. package/src/archiver/memory_archiver_store/l1_to_l2_message_store.ts +0 -61
  110. package/src/archiver/memory_archiver_store/memory_archiver_store.ts +0 -801
@@ -4,27 +4,40 @@ 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 { createEthereumChain } from '@aztec/ethereum';
7
+ import { EpochCache } from '@aztec/epoch-cache';
8
+ import { BlockTagTooOldError, InboxContract, RollupContract, createEthereumChain } from '@aztec/ethereum';
9
+ import { maxBigint } from '@aztec/foundation/bigint';
10
+ import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
11
+ import { merge, pick } from '@aztec/foundation/collection';
8
12
  import { Fr } from '@aztec/foundation/fields';
9
13
  import { createLogger } from '@aztec/foundation/log';
14
+ import { promiseWithResolvers } from '@aztec/foundation/promise';
10
15
  import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
11
16
  import { count } from '@aztec/foundation/string';
12
- import { elapsed } from '@aztec/foundation/timer';
13
- import { InboxAbi, RollupAbi } from '@aztec/l1-artifacts';
14
- import { ContractClassRegisteredEvent, PrivateFunctionBroadcastedEvent, UnconstrainedFunctionBroadcastedEvent } from '@aztec/protocol-contracts/class-registerer';
15
- import { ContractInstanceDeployedEvent, ContractInstanceUpdatedEvent } from '@aztec/protocol-contracts/instance-deployer';
16
- import { L2BlockSourceEvents } from '@aztec/stdlib/block';
17
- import { computePublicBytecodeCommitment, isValidPrivateFunctionMembershipProof, isValidUnconstrainedFunctionMembershipProof } from '@aztec/stdlib/contract';
17
+ import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
18
+ import { ContractClassPublishedEvent, PrivateFunctionBroadcastedEvent, UtilityFunctionBroadcastedEvent } from '@aztec/protocol-contracts/class-registry';
19
+ import { ContractInstancePublishedEvent, ContractInstanceUpdatedEvent } from '@aztec/protocol-contracts/instance-registry';
20
+ import { L2Block, L2BlockSourceEvents, PublishedL2Block } from '@aztec/stdlib/block';
21
+ import { computePublicBytecodeCommitment, isValidPrivateFunctionMembershipProof, isValidUtilityFunctionMembershipProof } from '@aztec/stdlib/contract';
18
22
  import { getEpochAtSlot, getEpochNumberAtTimestamp, getSlotAtTimestamp, getSlotRangeForEpoch, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
19
- import { Attributes, trackSpan } from '@aztec/telemetry-client';
23
+ import { getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
20
24
  import { EventEmitter } from 'events';
21
25
  import groupBy from 'lodash.groupby';
22
- import { createPublicClient, fallback, getContract, http } from 'viem';
23
- import { retrieveBlocksFromRollup, retrieveL1ToL2Messages } from './data_retrieval.js';
24
- import { NoBlobBodiesFoundError } from './errors.js';
26
+ import { createPublicClient, fallback, http } from 'viem';
27
+ import { retrieveCheckpointsFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedToPublishedCheckpoint } from './data_retrieval.js';
28
+ import { InitialBlockNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
25
29
  import { ArchiverInstrumentation } from './instrumentation.js';
30
+ import { validateCheckpointAttestations } from './validation.js';
31
+ function mapArchiverConfig(config) {
32
+ return {
33
+ pollingIntervalMs: config.archiverPollingIntervalMS,
34
+ batchSize: config.archiverBatchSize,
35
+ skipValidateBlockAttestations: config.skipValidateBlockAttestations,
36
+ maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds
37
+ };
38
+ }
26
39
  /**
27
- * Pulls L2 blocks in a non-blocking manner and provides interface for their retrieval.
40
+ * Pulls checkpoints in a non-blocking manner and provides interface for their retrieval.
28
41
  * Responsible for handling robust L1 polling so that other components do not need to
29
42
  * concern themselves with it.
30
43
  */ export class Archiver extends EventEmitter {
@@ -33,17 +46,19 @@ import { ArchiverInstrumentation } from './instrumentation.js';
33
46
  dataStore;
34
47
  config;
35
48
  blobSinkClient;
49
+ epochCache;
50
+ dateProvider;
36
51
  instrumentation;
37
52
  l1constants;
38
53
  log;
39
- /**
40
- * A promise in which we will be continually fetching new L2 blocks.
41
- */ runningPromise;
54
+ /** A loop in which we will be continually fetching new checkpoints. */ runningPromise;
42
55
  rollup;
43
56
  inbox;
44
57
  store;
45
58
  l1BlockNumber;
46
59
  l1Timestamp;
60
+ initialSyncComplete;
61
+ initialSyncPromise;
47
62
  tracer;
48
63
  /**
49
64
  * Creates a new instance of the Archiver.
@@ -54,20 +69,16 @@ import { ArchiverInstrumentation } from './instrumentation.js';
54
69
  * @param pollingIntervalMs - The interval for polling for L1 logs (in milliseconds).
55
70
  * @param store - An archiver data store for storage & retrieval of blocks, encrypted logs & contract data.
56
71
  * @param log - A logger.
57
- */ constructor(publicClient, l1Addresses, dataStore, config, blobSinkClient, instrumentation, l1constants, log = createLogger('archiver')){
58
- super(), this.publicClient = publicClient, this.l1Addresses = l1Addresses, this.dataStore = dataStore, this.config = config, this.blobSinkClient = blobSinkClient, this.instrumentation = instrumentation, this.l1constants = l1constants, this.log = log;
72
+ */ constructor(publicClient, l1Addresses, dataStore, config, blobSinkClient, epochCache, dateProvider, instrumentation, l1constants, log = createLogger('archiver')){
73
+ super(), this.publicClient = publicClient, this.l1Addresses = l1Addresses, this.dataStore = dataStore, this.config = config, this.blobSinkClient = blobSinkClient, this.epochCache = epochCache, this.dateProvider = dateProvider, this.instrumentation = instrumentation, this.l1constants = l1constants, this.log = log, this.initialSyncComplete = false;
59
74
  this.tracer = instrumentation.tracer;
60
75
  this.store = new ArchiverStoreHelper(dataStore);
61
- this.rollup = getContract({
62
- address: l1Addresses.rollupAddress.toString(),
63
- abi: RollupAbi,
64
- client: publicClient
65
- });
66
- this.inbox = getContract({
67
- address: l1Addresses.inboxAddress.toString(),
68
- abi: InboxAbi,
69
- client: publicClient
70
- });
76
+ this.rollup = new RollupContract(publicClient, l1Addresses.rollupAddress);
77
+ this.inbox = new InboxContract(publicClient, l1Addresses.inboxAddress);
78
+ this.initialSyncPromise = promiseWithResolvers();
79
+ // Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
80
+ // are done as fast as possible. This then gets updated once the initial sync completes.
81
+ this.runningPromise = new RunningPromise(()=>this.sync(), this.log, this.config.pollingIntervalMs / 10, makeLoggingErrorHandler(this.log, NoBlobBodiesFoundError, BlockTagTooOldError));
71
82
  }
72
83
  /**
73
84
  * Creates a new instance of the Archiver and blocks until it syncs from chain.
@@ -82,56 +93,88 @@ import { ArchiverInstrumentation } from './instrumentation.js';
82
93
  transport: fallback(config.l1RpcUrls.map((url)=>http(url))),
83
94
  pollingInterval: config.viemPollingIntervalMS
84
95
  });
85
- const rollup = getContract({
86
- address: config.l1Contracts.rollupAddress.toString(),
87
- abi: RollupAbi,
88
- client: publicClient
89
- });
90
- const [l1StartBlock, l1GenesisTime] = await Promise.all([
91
- rollup.read.L1_BLOCK_AT_GENESIS(),
92
- rollup.read.getGenesisTime()
96
+ const rollup = new RollupContract(publicClient, config.l1Contracts.rollupAddress);
97
+ const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot] = await Promise.all([
98
+ rollup.getL1StartBlock(),
99
+ rollup.getL1GenesisTime(),
100
+ rollup.getProofSubmissionEpochs(),
101
+ rollup.getGenesisArchiveTreeRoot()
93
102
  ]);
103
+ const l1StartBlockHash = await publicClient.getBlock({
104
+ blockNumber: l1StartBlock,
105
+ includeTransactions: false
106
+ }).then((block)=>Buffer32.fromString(block.hash));
94
107
  const { aztecEpochDuration: epochDuration, aztecSlotDuration: slotDuration, ethereumSlotDuration } = config;
95
- const archiver = new Archiver(publicClient, config.l1Contracts, archiverStore, {
96
- pollingIntervalMs: config.archiverPollingIntervalMS ?? 10_000,
97
- batchSize: config.archiverBatchSize ?? 100
98
- }, deps.blobSinkClient, await ArchiverInstrumentation.new(deps.telemetry, ()=>archiverStore.estimateSize()), {
108
+ const l1Constants = {
109
+ l1StartBlockHash,
99
110
  l1StartBlock,
100
111
  l1GenesisTime,
101
112
  epochDuration,
102
113
  slotDuration,
103
- ethereumSlotDuration
104
- });
114
+ ethereumSlotDuration,
115
+ proofSubmissionEpochs: Number(proofSubmissionEpochs),
116
+ genesisArchiveRoot: Fr.fromHexString(genesisArchiveRoot)
117
+ };
118
+ const opts = merge({
119
+ pollingIntervalMs: 10_000,
120
+ batchSize: 100,
121
+ maxAllowedEthClientDriftSeconds: 300
122
+ }, mapArchiverConfig(config));
123
+ const epochCache = deps.epochCache ?? await EpochCache.create(config.l1Contracts.rollupAddress, config, deps);
124
+ const telemetry = deps.telemetry ?? getTelemetryClient();
125
+ const archiver = new Archiver(publicClient, config.l1Contracts, archiverStore, opts, deps.blobSinkClient, epochCache, deps.dateProvider ?? new DateProvider(), await ArchiverInstrumentation.new(telemetry, ()=>archiverStore.estimateSize()), l1Constants);
105
126
  await archiver.start(blockUntilSynced);
106
127
  return archiver;
107
128
  }
129
+ /** Updates archiver config */ updateConfig(newConfig) {
130
+ this.config = merge(this.config, mapArchiverConfig(newConfig));
131
+ }
108
132
  /**
109
133
  * Starts sync process.
110
134
  * @param blockUntilSynced - If true, blocks until the archiver has fully synced.
111
135
  */ async start(blockUntilSynced) {
112
- if (this.runningPromise) {
136
+ if (this.runningPromise.isRunning()) {
113
137
  throw new Error('Archiver is already running');
114
138
  }
139
+ await this.blobSinkClient.testSources();
140
+ await this.testEthereumNodeSynced();
141
+ // Log initial state for the archiver
142
+ const { l1StartBlock } = this.l1constants;
143
+ const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
144
+ const currentL2Block = await this.getBlockNumber();
145
+ this.log.info(`Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo} and L2 block ${currentL2Block}`, {
146
+ blocksSynchedTo,
147
+ messagesSynchedTo,
148
+ currentL2Block
149
+ });
150
+ // Start sync loop, and return the wait for initial sync if we are asked to block until synced
151
+ this.runningPromise.start();
115
152
  if (blockUntilSynced) {
116
- await this.syncSafe(blockUntilSynced);
153
+ return this.waitForInitialSync();
117
154
  }
118
- this.runningPromise = new RunningPromise(()=>this.sync(false), this.log, this.config.pollingIntervalMs, makeLoggingErrorHandler(this.log, // Ignored errors will not log to the console
119
- // We ignore NoBlobBodiesFound as the message may not have been passed to the blob sink yet
120
- NoBlobBodiesFoundError));
121
- this.runningPromise.start();
122
155
  }
123
- async syncSafe(initialRun) {
124
- try {
125
- await this.sync(initialRun);
126
- } catch (error) {
127
- this.log.error('Error during sync', {
128
- error
129
- });
156
+ syncImmediate() {
157
+ return this.runningPromise.trigger();
158
+ }
159
+ waitForInitialSync() {
160
+ return this.initialSyncPromise.promise;
161
+ }
162
+ /** Checks that the ethereum node we are connected to has a latest timestamp no more than the allowed drift. Throw if not. */ async testEthereumNodeSynced() {
163
+ const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
164
+ if (maxAllowedDelay === 0) {
165
+ return;
166
+ }
167
+ const { number, timestamp: l1Timestamp } = await this.publicClient.getBlock({
168
+ includeTransactions: false
169
+ });
170
+ const currentTime = BigInt(this.dateProvider.nowInSeconds());
171
+ if (currentTime - l1Timestamp > BigInt(maxAllowedDelay)) {
172
+ throw new Error(`Ethereum node is out of sync (last block synced ${number} at ${l1Timestamp} vs current time ${currentTime})`);
130
173
  }
131
174
  }
132
175
  /**
133
176
  * Fetches logs from L1 contracts and processes them.
134
- */ async sync(initialRun) {
177
+ */ async sync() {
135
178
  /**
136
179
  * We keep track of three "pointers" to L1 blocks:
137
180
  * 1. the last L1 block that published an L2 block
@@ -141,14 +184,22 @@ import { ArchiverInstrumentation } from './instrumentation.js';
141
184
  * We do this to deal with L1 data providers that are eventually consistent (e.g. Infura).
142
185
  * We guard against seeing block X with no data at one point, and later, the provider processes the block and it has data.
143
186
  * The archiver will stay back, until there's data on L1 that will move the pointers forward.
144
- *
145
- * This code does not handle reorgs.
146
- */ const { l1StartBlock } = this.l1constants;
147
- const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
148
- const currentL1BlockNumber = await this.publicClient.getBlockNumber();
149
- if (initialRun) {
150
- this.log.info(`Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${Math.min(Number(blocksSynchedTo), Number(messagesSynchedTo))} to current L1 block ${currentL1BlockNumber}`);
151
- }
187
+ */ const { l1StartBlock, l1StartBlockHash } = this.l1constants;
188
+ const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = {
189
+ l1BlockNumber: l1StartBlock,
190
+ l1BlockHash: l1StartBlockHash
191
+ } } = await this.store.getSynchPoint();
192
+ const currentL1Block = await this.publicClient.getBlock({
193
+ includeTransactions: false
194
+ });
195
+ const currentL1BlockNumber = currentL1Block.number;
196
+ const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
197
+ this.log.trace(`Starting new archiver sync iteration`, {
198
+ blocksSynchedTo,
199
+ messagesSynchedTo,
200
+ currentL1BlockNumber,
201
+ currentL1BlockHash
202
+ });
152
203
  // ********** Ensuring Consistency of data pulled from L1 **********
153
204
  /**
154
205
  * There are a number of calls in this sync operation to L1 for retrieving
@@ -166,64 +217,112 @@ import { ArchiverInstrumentation } from './instrumentation.js';
166
217
  * data up to the currentBlockNumber captured at the top of this function. We might want to improve on this
167
218
  * in future but for the time being it should give us the guarantees that we need
168
219
  */ // ********** Events that are processed per L1 block **********
169
- await this.handleL1ToL2Messages(messagesSynchedTo, currentL1BlockNumber);
170
- // Store latest l1 block number and timestamp seen. Used for epoch and slots calculations.
171
- if (!this.l1BlockNumber || this.l1BlockNumber < currentL1BlockNumber) {
172
- this.l1Timestamp = (await this.publicClient.getBlock({
173
- blockNumber: currentL1BlockNumber
174
- })).timestamp;
175
- this.l1BlockNumber = currentL1BlockNumber;
176
- }
177
- // ********** Events that are processed per L2 block **********
220
+ await this.handleL1ToL2Messages(messagesSynchedTo, currentL1BlockNumber, currentL1BlockHash);
221
+ // Get L1 timestamp for the current block
222
+ const currentL1Timestamp = !this.l1Timestamp || !this.l1BlockNumber || this.l1BlockNumber !== currentL1BlockNumber ? (await this.publicClient.getBlock({
223
+ blockNumber: currentL1BlockNumber
224
+ })).timestamp : this.l1Timestamp;
225
+ // Warn if the latest L1 block timestamp is too old
226
+ const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
227
+ const now = this.dateProvider.nowInSeconds();
228
+ if (maxAllowedDelay > 0 && Number(currentL1Timestamp) <= now - maxAllowedDelay) {
229
+ this.log.warn(`Latest L1 block ${currentL1BlockNumber} timestamp ${currentL1Timestamp} is too old. Make sure your Ethereum node is synced.`, {
230
+ currentL1BlockNumber,
231
+ currentL1Timestamp,
232
+ now,
233
+ maxAllowedDelay
234
+ });
235
+ }
236
+ // ********** Events that are processed per checkpoint **********
178
237
  if (currentL1BlockNumber > blocksSynchedTo) {
179
- // First we retrieve new L2 blocks
180
- const { provenBlockNumber } = await this.handleL2blocks(blocksSynchedTo, currentL1BlockNumber);
181
- // And then we prune the current epoch if it'd reorg on next submission.
182
- // Note that we don't do this before retrieving L2 blocks because we may need to retrieve
183
- // blocks from more than 2 epochs ago, so we want to make sure we have the latest view of
238
+ // First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
239
+ // pending chain validation status, proven checkpoint number, and synched L1 block number.
240
+ const rollupStatus = await this.handleCheckpoints(blocksSynchedTo, currentL1BlockNumber);
241
+ // Then we prune the current epoch if it'd reorg on next submission.
242
+ // Note that we don't do this before retrieving checkpoints because we may need to retrieve
243
+ // checkpoints from more than 2 epochs ago, so we want to make sure we have the latest view of
184
244
  // the chain locally before we start unwinding stuff. This can be optimized by figuring out
185
- // up to which point we're pruning, and then requesting L2 blocks up to that point only.
186
- await this.handleEpochPrune(provenBlockNumber, currentL1BlockNumber);
245
+ // up to which point we're pruning, and then requesting checkpoints up to that point only.
246
+ const { rollupCanPrune } = await this.handleEpochPrune(rollupStatus.provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp);
247
+ // If the last checkpoint we processed had an invalid attestation, we manually advance the L1 syncpoint
248
+ // past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
249
+ // we get a valid checkpoint to advance the syncpoint.
250
+ if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
251
+ await this.store.setBlockSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
252
+ }
253
+ // And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
254
+ // We only do this if rollup cant prune on the next submission. Otherwise we will end up
255
+ // re-syncing the checkpoints we have just unwound above. We also dont do this if the last checkpoint is invalid,
256
+ // since the archiver will rightfully refuse to sync up to it.
257
+ if (!rollupCanPrune && rollupStatus.validationResult?.valid) {
258
+ await this.checkForNewCheckpointsBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
259
+ }
187
260
  this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
188
261
  }
189
- if (initialRun) {
190
- this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete.`, {
262
+ // After syncing has completed, update the current l1 block number and timestamp,
263
+ // otherwise we risk announcing to the world that we've synced to a given point,
264
+ // but the corresponding blocks have not been processed (see #12631).
265
+ this.l1Timestamp = currentL1Timestamp;
266
+ this.l1BlockNumber = currentL1BlockNumber;
267
+ // We resolve the initial sync only once we've caught up with the latest L1 block number (with 1 block grace)
268
+ // so if the initial sync took too long, we still go for another iteration.
269
+ if (!this.initialSyncComplete && currentL1BlockNumber + 1n >= await this.publicClient.getBlockNumber()) {
270
+ this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete`, {
191
271
  l1BlockNumber: currentL1BlockNumber,
192
272
  syncPoint: await this.store.getSynchPoint(),
193
273
  ...await this.getL2Tips()
194
274
  });
275
+ this.runningPromise.setPollingIntervalMS(this.config.pollingIntervalMs);
276
+ this.initialSyncComplete = true;
277
+ this.initialSyncPromise.resolve();
195
278
  }
196
279
  }
197
- /** Queries the rollup contract on whether a prune can be executed on the immediatenext L1 block. */ async canPrune(currentL1BlockNumber) {
198
- const time = (this.l1Timestamp ?? 0n) + BigInt(this.l1constants.ethereumSlotDuration);
199
- return await this.rollup.read.canPruneAtTime([
200
- time
201
- ], {
280
+ /** Queries the rollup contract on whether a prune can be executed on the immediate next L1 block. */ async canPrune(currentL1BlockNumber, currentL1Timestamp) {
281
+ const time = (currentL1Timestamp ?? 0n) + BigInt(this.l1constants.ethereumSlotDuration);
282
+ const result = await this.rollup.canPruneAtTime(time, {
202
283
  blockNumber: currentL1BlockNumber
203
284
  });
285
+ if (result) {
286
+ this.log.debug(`Rollup contract allows pruning at L1 block ${currentL1BlockNumber} time ${time}`, {
287
+ currentL1Timestamp,
288
+ pruneTime: time,
289
+ currentL1BlockNumber
290
+ });
291
+ }
292
+ return result;
204
293
  }
205
- /** Checks if there'd be a reorg for the next block submission and start pruning now. */ async handleEpochPrune(provenBlockNumber, currentL1BlockNumber) {
206
- const localPendingBlockNumber = BigInt(await this.getBlockNumber());
207
- const canPrune = localPendingBlockNumber > provenBlockNumber && await this.canPrune(currentL1BlockNumber);
294
+ /** Checks if there'd be a reorg for the next checkpoint submission and start pruning now. */ async handleEpochPrune(provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp) {
295
+ const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
296
+ const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
297
+ const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
208
298
  if (canPrune) {
209
- const localPendingSlotNumber = await this.getL2SlotNumber();
210
- const localPendingEpochNumber = getEpochAtSlot(localPendingSlotNumber, this.l1constants);
299
+ const timer = new Timer();
300
+ const pruneFrom = provenCheckpointNumber + 1;
301
+ const header = await this.getCheckpointHeader(Number(pruneFrom));
302
+ if (header === undefined) {
303
+ throw new Error(`Missing checkpoint header ${pruneFrom}`);
304
+ }
305
+ const pruneFromSlotNumber = header.slotNumber;
306
+ const pruneFromEpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
307
+ const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
308
+ const checkpoints = await this.getCheckpoints(Number(provenCheckpointNumber) + 1, Number(checkpointsToUnwind));
211
309
  // Emit an event for listening services to react to the chain prune
212
310
  this.emit(L2BlockSourceEvents.L2PruneDetected, {
213
311
  type: L2BlockSourceEvents.L2PruneDetected,
214
- blockNumber: localPendingBlockNumber,
215
- slotNumber: localPendingSlotNumber,
216
- epochNumber: localPendingEpochNumber
312
+ epochNumber: pruneFromEpochNumber,
313
+ blocks: checkpoints.flatMap((c)=>L2Block.fromCheckpoint(c))
217
314
  });
218
- const blocksToUnwind = localPendingBlockNumber - provenBlockNumber;
219
- this.log.debug(`L2 prune from ${provenBlockNumber + 1n} to ${localPendingBlockNumber} will occur on next block submission.`);
220
- await this.store.unwindBlocks(Number(localPendingBlockNumber), Number(blocksToUnwind));
221
- 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()}.`);
222
- this.instrumentation.processPrune();
315
+ this.log.debug(`L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`);
316
+ await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
317
+ 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()}.`);
318
+ this.instrumentation.processPrune(timer.ms());
223
319
  // TODO(palla/reorg): Do we need to set the block synched L1 block number here?
224
320
  // Seems like the next iteration should handle this.
225
321
  // await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
226
322
  }
323
+ return {
324
+ rollupCanPrune
325
+ };
227
326
  }
228
327
  nextRange(end, limit) {
229
328
  const batchSize = this.config.batchSize * this.l1constants.slotDuration / this.l1constants.ethereumSlotDuration;
@@ -240,167 +339,431 @@ import { ArchiverInstrumentation } from './instrumentation.js';
240
339
  nextEnd
241
340
  ];
242
341
  }
243
- async handleL1ToL2Messages(messagesSynchedTo, currentL1BlockNumber) {
244
- this.log.trace(`Handling L1 to L2 messages from ${messagesSynchedTo} to ${currentL1BlockNumber}.`);
245
- if (currentL1BlockNumber <= messagesSynchedTo) {
342
+ async handleL1ToL2Messages(messagesSyncPoint, currentL1BlockNumber, _currentL1BlockHash) {
343
+ this.log.trace(`Handling L1 to L2 messages from ${messagesSyncPoint.l1BlockNumber} to ${currentL1BlockNumber}.`);
344
+ if (currentL1BlockNumber <= messagesSyncPoint.l1BlockNumber) {
246
345
  return;
247
346
  }
248
- const localTotalMessageCount = await this.store.getTotalL1ToL2MessageCount();
249
- const destinationTotalMessageCount = await this.inbox.read.totalMessagesInserted();
250
- if (localTotalMessageCount === destinationTotalMessageCount) {
251
- await this.store.setMessageSynchedL1BlockNumber(currentL1BlockNumber);
252
- this.log.trace(`Retrieved no new L1 to L2 messages between L1 blocks ${messagesSynchedTo + 1n} and ${currentL1BlockNumber}.`);
347
+ // Load remote and local inbox states.
348
+ const localMessagesInserted = await this.store.getTotalL1ToL2MessageCount();
349
+ const localLastMessage = await this.store.getLastL1ToL2Message();
350
+ const remoteMessagesState = await this.inbox.getState({
351
+ blockNumber: currentL1BlockNumber
352
+ });
353
+ this.log.trace(`Retrieved remote inbox state at L1 block ${currentL1BlockNumber}.`, {
354
+ localMessagesInserted,
355
+ localLastMessage,
356
+ remoteMessagesState
357
+ });
358
+ // Compare message count and rolling hash. If they match, no need to retrieve anything.
359
+ if (remoteMessagesState.totalMessagesInserted === localMessagesInserted && remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO)) {
360
+ this.log.trace(`No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`);
253
361
  return;
254
362
  }
255
- // Retrieve messages in batches. Each batch is estimated to acommodate up to L2 'blockBatchSize' blocks,
256
- let searchStartBlock = messagesSynchedTo;
257
- let searchEndBlock = messagesSynchedTo;
363
+ // Check if our syncpoint is still valid. If not, there was an L1 reorg and we need to re-retrieve messages.
364
+ // Note that we need to fetch it from logs and not from inbox state at the syncpoint l1 block number, since it
365
+ // could be older than 128 blocks and non-archive nodes cannot resolve it.
366
+ if (localLastMessage) {
367
+ const remoteLastMessage = await this.retrieveL1ToL2Message(localLastMessage.leaf);
368
+ this.log.trace(`Retrieved remote message for local last`, {
369
+ remoteLastMessage,
370
+ localLastMessage
371
+ });
372
+ if (!remoteLastMessage || !remoteLastMessage.rollingHash.equals(localLastMessage.rollingHash)) {
373
+ this.log.warn(`Rolling back L1 to L2 messages due to hash mismatch or msg not found.`, {
374
+ remoteLastMessage,
375
+ messagesSyncPoint,
376
+ localLastMessage
377
+ });
378
+ messagesSyncPoint = await this.rollbackL1ToL2Messages(localLastMessage, messagesSyncPoint);
379
+ this.log.debug(`Rolled back L1 to L2 messages to L1 block ${messagesSyncPoint.l1BlockNumber}.`, {
380
+ messagesSyncPoint
381
+ });
382
+ }
383
+ }
384
+ // Retrieve and save messages in batches. Each batch is estimated to acommodate up to L2 'blockBatchSize' blocks,
385
+ let searchStartBlock = 0n;
386
+ let searchEndBlock = messagesSyncPoint.l1BlockNumber;
387
+ let lastMessage;
388
+ let messageCount = 0;
258
389
  do {
259
390
  [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
260
391
  this.log.trace(`Retrieving L1 to L2 messages between L1 blocks ${searchStartBlock} and ${searchEndBlock}.`);
261
- const retrievedL1ToL2Messages = await retrieveL1ToL2Messages(this.inbox, searchStartBlock, searchEndBlock);
262
- this.log.verbose(`Retrieved ${retrievedL1ToL2Messages.retrievedData.length} new L1 to L2 messages between L1 blocks ${searchStartBlock} and ${searchEndBlock}.`);
263
- await this.store.addL1ToL2Messages(retrievedL1ToL2Messages);
264
- for (const msg of retrievedL1ToL2Messages.retrievedData){
392
+ const messages = await retrieveL1ToL2Messages(this.inbox.getContract(), searchStartBlock, searchEndBlock);
393
+ this.log.verbose(`Retrieved ${messages.length} new L1 to L2 messages between L1 blocks ${searchStartBlock} and ${searchEndBlock}.`);
394
+ const timer = new Timer();
395
+ await this.store.addL1ToL2Messages(messages);
396
+ const perMsg = timer.ms() / messages.length;
397
+ this.instrumentation.processNewMessages(messages.length, perMsg);
398
+ for (const msg of messages){
265
399
  this.log.debug(`Downloaded L1 to L2 message`, {
266
- leaf: msg.leaf.toString(),
267
- index: msg.index
400
+ ...msg,
401
+ leaf: msg.leaf.toString()
268
402
  });
403
+ lastMessage = msg;
404
+ messageCount++;
269
405
  }
270
406
  }while (searchEndBlock < currentL1BlockNumber)
407
+ // Log stats for messages retrieved (if any).
408
+ if (messageCount > 0) {
409
+ this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for L2 block ${lastMessage?.l2BlockNumber}`, {
410
+ lastMessage,
411
+ messageCount
412
+ });
413
+ }
414
+ // Warn if the resulting rolling hash does not match the remote state we had retrieved.
415
+ if (lastMessage && !lastMessage.rollingHash.equals(remoteMessagesState.messagesRollingHash)) {
416
+ this.log.warn(`Last message retrieved rolling hash does not match remote state.`, {
417
+ lastMessage,
418
+ remoteMessagesState
419
+ });
420
+ }
271
421
  }
272
- async handleL2blocks(blocksSynchedTo, currentL1BlockNumber) {
273
- const localPendingBlockNumber = BigInt(await this.getBlockNumber());
274
- const [provenBlockNumber, provenArchive, pendingBlockNumber, pendingArchive, archiveForLocalPendingBlockNumber] = await this.rollup.read.status([
275
- localPendingBlockNumber
276
- ], {
422
+ async retrieveL1ToL2Message(leaf) {
423
+ const currentL1BlockNumber = await this.publicClient.getBlockNumber();
424
+ let searchStartBlock = 0n;
425
+ let searchEndBlock = this.l1constants.l1StartBlock - 1n;
426
+ do {
427
+ [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
428
+ const message = await retrieveL1ToL2Message(this.inbox.getContract(), leaf, searchStartBlock, searchEndBlock);
429
+ if (message) {
430
+ return message;
431
+ }
432
+ }while (searchEndBlock < currentL1BlockNumber)
433
+ return undefined;
434
+ }
435
+ async rollbackL1ToL2Messages(localLastMessage, messagesSyncPoint) {
436
+ // Slowly go back through our messages until we find the last common message.
437
+ // We could query the logs in batch as an optimization, but the depth of the reorg should not be deep, and this
438
+ // is a very rare case, so it's fine to query one log at a time.
439
+ let commonMsg;
440
+ this.log.verbose(`Searching most recent common L1 to L2 message at or before index ${localLastMessage.index}`);
441
+ for await (const msg of this.store.iterateL1ToL2Messages({
442
+ reverse: true,
443
+ end: localLastMessage.index
444
+ })){
445
+ const remoteMsg = await this.retrieveL1ToL2Message(msg.leaf);
446
+ const logCtx = {
447
+ remoteMsg,
448
+ localMsg: msg
449
+ };
450
+ if (remoteMsg && remoteMsg.rollingHash.equals(msg.rollingHash)) {
451
+ this.log.verbose(`Found most recent common L1 to L2 message at index ${msg.index} on L1 block ${msg.l1BlockNumber}`, logCtx);
452
+ commonMsg = remoteMsg;
453
+ break;
454
+ } else if (remoteMsg) {
455
+ this.log.debug(`Local L1 to L2 message with index ${msg.index} has different rolling hash`, logCtx);
456
+ } else {
457
+ this.log.debug(`Local L1 to L2 message with index ${msg.index} not found on L1`, logCtx);
458
+ }
459
+ }
460
+ // Delete everything after the common message we found.
461
+ const lastGoodIndex = commonMsg?.index;
462
+ this.log.warn(`Deleting all local L1 to L2 messages after index ${lastGoodIndex ?? 'undefined'}`);
463
+ await this.store.removeL1ToL2Messages(lastGoodIndex !== undefined ? lastGoodIndex + 1n : 0n);
464
+ // Update the syncpoint so the loop below reprocesses the changed messages. We go to the block before
465
+ // the last common one, so we force reprocessing it, in case new messages were added on that same L1 block
466
+ // after the last common message.
467
+ const syncPointL1BlockNumber = commonMsg ? commonMsg.l1BlockNumber - 1n : this.l1constants.l1StartBlock;
468
+ const syncPointL1BlockHash = await this.getL1BlockHash(syncPointL1BlockNumber);
469
+ messagesSyncPoint = {
470
+ l1BlockNumber: syncPointL1BlockNumber,
471
+ l1BlockHash: syncPointL1BlockHash
472
+ };
473
+ await this.store.setMessageSynchedL1Block(messagesSyncPoint);
474
+ return messagesSyncPoint;
475
+ }
476
+ async getL1BlockHash(l1BlockNumber) {
477
+ const block = await this.publicClient.getBlock({
478
+ blockNumber: l1BlockNumber,
479
+ includeTransactions: false
480
+ });
481
+ if (!block) {
482
+ throw new Error(`Missing L1 block ${l1BlockNumber}`);
483
+ }
484
+ return Buffer32.fromString(block.hash);
485
+ }
486
+ async handleCheckpoints(blocksSynchedTo, currentL1BlockNumber) {
487
+ const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
488
+ const initialValidationResult = await this.store.getPendingChainValidationStatus();
489
+ const [rollupProvenCheckpointNumber, provenArchive, rollupPendingCheckpointNumber, pendingArchive, archiveForLocalPendingCheckpointNumber] = await this.rollup.status(BigInt(localPendingCheckpointNumber), {
277
490
  blockNumber: currentL1BlockNumber
278
491
  });
279
- const updateProvenBlock = async ()=>{
280
- const localBlockForDestinationProvenBlockNumber = await this.getBlock(Number(provenBlockNumber));
281
- // Sanity check. I've hit what seems to be a state where the proven block is set to a value greater than the latest
282
- // synched block when requesting L2Tips from the archiver. This is the only place where the proven block is set.
283
- const synched = await this.store.getSynchedL2BlockNumber();
284
- if (localBlockForDestinationProvenBlockNumber && synched < localBlockForDestinationProvenBlockNumber?.number) {
285
- this.log.error(`Hit local block greater than last synched block: ${localBlockForDestinationProvenBlockNumber.number} > ${synched}`);
492
+ const provenCheckpointNumber = Number(rollupProvenCheckpointNumber);
493
+ const pendingCheckpointNumber = Number(rollupPendingCheckpointNumber);
494
+ const rollupStatus = {
495
+ provenCheckpointNumber,
496
+ provenArchive,
497
+ pendingCheckpointNumber,
498
+ pendingArchive,
499
+ validationResult: initialValidationResult
500
+ };
501
+ this.log.trace(`Retrieved rollup status at current L1 block ${currentL1BlockNumber}.`, {
502
+ localPendingCheckpointNumber,
503
+ blocksSynchedTo,
504
+ currentL1BlockNumber,
505
+ archiveForLocalPendingCheckpointNumber,
506
+ ...rollupStatus
507
+ });
508
+ const updateProvenCheckpoint = async ()=>{
509
+ // Annoying edge case: if proven checkpoint is moved back to 0 due to a reorg at the beginning of the chain,
510
+ // we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
511
+ // so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
512
+ if (provenCheckpointNumber === 0) {
513
+ const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
514
+ if (localProvenCheckpointNumber !== provenCheckpointNumber) {
515
+ await this.setProvenCheckpointNumber(provenCheckpointNumber);
516
+ this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, {
517
+ provenCheckpointNumber
518
+ });
519
+ }
286
520
  }
287
- if (localBlockForDestinationProvenBlockNumber && provenArchive === localBlockForDestinationProvenBlockNumber.archive.root.toString()) {
288
- const localProvenBlockNumber = await this.store.getProvenL2BlockNumber();
289
- if (localProvenBlockNumber !== Number(provenBlockNumber)) {
290
- await this.store.setProvenL2BlockNumber(Number(provenBlockNumber));
291
- this.log.info(`Updated proven chain to block ${provenBlockNumber}`, {
292
- provenBlockNumber
521
+ const localCheckpointForDestinationProvenCheckpointNumber = await this.getCheckpoint(provenCheckpointNumber);
522
+ // Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
523
+ // synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
524
+ const synched = await this.getSynchedCheckpointNumber();
525
+ if (localCheckpointForDestinationProvenCheckpointNumber && synched < localCheckpointForDestinationProvenCheckpointNumber.number) {
526
+ this.log.error(`Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.number} > ${synched}`);
527
+ }
528
+ this.log.trace(`Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'}`);
529
+ const lastProvenBlockNumber = await this.getLastBlockNumberInCheckpoint(provenCheckpointNumber);
530
+ if (localCheckpointForDestinationProvenCheckpointNumber && provenArchive === localCheckpointForDestinationProvenCheckpointNumber.archive.root.toString()) {
531
+ const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
532
+ if (localProvenCheckpointNumber !== provenCheckpointNumber) {
533
+ await this.setProvenCheckpointNumber(provenCheckpointNumber);
534
+ this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, {
535
+ provenCheckpointNumber
536
+ });
537
+ const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
538
+ const provenEpochNumber = getEpochAtSlot(provenSlotNumber, this.l1constants);
539
+ this.emit(L2BlockSourceEvents.L2BlockProven, {
540
+ type: L2BlockSourceEvents.L2BlockProven,
541
+ blockNumber: BigInt(lastProvenBlockNumber),
542
+ slotNumber: provenSlotNumber,
543
+ epochNumber: provenEpochNumber
293
544
  });
545
+ } else {
546
+ this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
294
547
  }
295
548
  }
296
- this.instrumentation.updateLastProvenBlock(Number(provenBlockNumber));
549
+ this.instrumentation.updateLastProvenBlock(lastProvenBlockNumber);
297
550
  };
298
- // This is an edge case that we only hit if there are no proposed blocks.
299
- // If we have 0 blocks locally and there are no blocks onchain there is nothing to do.
300
- const noBlocks = localPendingBlockNumber === 0n && pendingBlockNumber === 0n;
301
- if (noBlocks) {
551
+ // This is an edge case that we only hit if there are no proposed checkpoints.
552
+ // If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
553
+ const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
554
+ if (noCheckpoints) {
302
555
  await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
303
- this.log.debug(`No blocks to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
304
- return {
305
- provenBlockNumber
306
- };
556
+ this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`);
557
+ return rollupStatus;
307
558
  }
308
- await updateProvenBlock();
559
+ await updateProvenCheckpoint();
309
560
  // Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
310
- // are any state that could be impacted by it. If we have no blocks, there is no impact.
311
- if (localPendingBlockNumber > 0) {
312
- const localPendingBlock = await this.getBlock(Number(localPendingBlockNumber));
313
- if (localPendingBlock === undefined) {
314
- throw new Error(`Missing block ${localPendingBlockNumber}`);
561
+ // are any state that could be impacted by it. If we have no checkpoints, there is no impact.
562
+ if (localPendingCheckpointNumber > 0) {
563
+ const localPendingCheckpoint = await this.getCheckpoint(localPendingCheckpointNumber);
564
+ if (localPendingCheckpoint === undefined) {
565
+ throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
315
566
  }
316
- const noBlockSinceLast = localPendingBlock && pendingArchive === localPendingBlock.archive.root.toString();
317
- if (noBlockSinceLast) {
318
- await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
319
- this.log.debug(`No blocks to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
320
- return {
321
- provenBlockNumber
322
- };
567
+ const localPendingArchiveRoot = localPendingCheckpoint.archive.root.toString();
568
+ const noCheckpointSinceLast = localPendingCheckpoint && pendingArchive === localPendingArchiveRoot;
569
+ if (noCheckpointSinceLast) {
570
+ // We believe the following line causes a problem when we encounter L1 re-orgs.
571
+ // Basically, by setting the synched L1 block number here, we are saying that we have
572
+ // processed all checkpoints up to the current L1 block number and we will not attempt to retrieve logs from
573
+ // this block again (or any blocks before).
574
+ // However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing checkpoints.
575
+ // We must only set this block number based on actually retrieved logs.
576
+ // TODO(#8621): Tackle this properly when we handle L1 Re-orgs.
577
+ // await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
578
+ this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
579
+ return rollupStatus;
323
580
  }
324
- const localPendingBlockInChain = archiveForLocalPendingBlockNumber === localPendingBlock.archive.root.toString();
325
- if (!localPendingBlockInChain) {
326
- // If our local pending block tip is not in the chain on L1 a "prune" must have happened
581
+ const localPendingCheckpointInChain = archiveForLocalPendingCheckpointNumber === localPendingArchiveRoot;
582
+ if (!localPendingCheckpointInChain) {
583
+ // If our local pending checkpoint tip is not in the chain on L1 a "prune" must have happened
327
584
  // or the L1 have reorged.
328
585
  // In any case, we have to figure out how far into the past the action will take us.
329
- // For simplicity here, we will simply rewind until we end in a block that is also on the chain on L1.
330
- this.log.debug(`L2 prune has been detected.`);
331
- let tipAfterUnwind = localPendingBlockNumber;
586
+ // For simplicity here, we will simply rewind until we end in a checkpoint that is also on the chain on L1.
587
+ this.log.debug(`L2 prune has been detected due to local pending checkpoint ${localPendingCheckpointNumber} not in chain`, {
588
+ localPendingCheckpointNumber,
589
+ localPendingArchiveRoot,
590
+ archiveForLocalPendingCheckpointNumber
591
+ });
592
+ let tipAfterUnwind = localPendingCheckpointNumber;
332
593
  while(true){
333
- const candidateBlock = await this.getBlock(Number(tipAfterUnwind));
334
- if (candidateBlock === undefined) {
594
+ const candidateCheckpoint = await this.getCheckpoint(tipAfterUnwind);
595
+ if (candidateCheckpoint === undefined) {
335
596
  break;
336
597
  }
337
- const archiveAtContract = await this.rollup.read.archiveAt([
338
- BigInt(candidateBlock.number)
339
- ]);
340
- if (archiveAtContract === candidateBlock.archive.root.toString()) {
598
+ const archiveAtContract = await this.rollup.archiveAt(BigInt(candidateCheckpoint.number));
599
+ this.log.trace(`Checking local checkpoint ${candidateCheckpoint.number} with archive ${candidateCheckpoint.archive.root}`, {
600
+ archiveAtContract,
601
+ archiveLocal: candidateCheckpoint.archive.root.toString()
602
+ });
603
+ if (archiveAtContract === candidateCheckpoint.archive.root.toString()) {
341
604
  break;
342
605
  }
343
606
  tipAfterUnwind--;
344
607
  }
345
- const blocksToUnwind = localPendingBlockNumber - tipAfterUnwind;
346
- await this.store.unwindBlocks(Number(localPendingBlockNumber), Number(blocksToUnwind));
347
- 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()}.`);
608
+ const checkpointsToUnwind = localPendingCheckpointNumber - tipAfterUnwind;
609
+ await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
610
+ 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()}.`);
348
611
  }
349
612
  }
350
- // Retrieve L2 blocks in batches. Each batch is estimated to acommodate up to L2 'blockBatchSize' blocks,
613
+ // Retrieve checkpoints in batches. Each batch is estimated to accommodate up to 'blockBatchSize' L1 blocks,
351
614
  // computed using the L2 block time vs the L1 block time.
352
615
  let searchStartBlock = blocksSynchedTo;
353
616
  let searchEndBlock = blocksSynchedTo;
617
+ let lastRetrievedCheckpoint;
618
+ let lastL1BlockWithCheckpoint = undefined;
354
619
  do {
355
620
  [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
356
- this.log.trace(`Retrieving L2 blocks from L1 block ${searchStartBlock} to ${searchEndBlock}`);
357
- // TODO(md): Retreive from blob sink then from consensus client, then from peers
358
- const retrievedBlocks = await retrieveBlocksFromRollup(this.rollup, this.publicClient, this.blobSinkClient, searchStartBlock, searchEndBlock, this.log);
359
- if (retrievedBlocks.length === 0) {
621
+ this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
622
+ // TODO(md): Retrieve from blob sink then from consensus client, then from peers
623
+ const retrievedCheckpoints = await retrieveCheckpointsFromRollup(this.rollup.getContract(), this.publicClient, this.blobSinkClient, searchStartBlock, searchEndBlock, this.log);
624
+ if (retrievedCheckpoints.length === 0) {
360
625
  // We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
361
626
  // See further details in earlier comments.
362
- this.log.trace(`Retrieved no new L2 blocks from L1 block ${searchStartBlock} to ${searchEndBlock}`);
627
+ this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
363
628
  continue;
364
629
  }
365
- const lastProcessedL1BlockNumber = retrievedBlocks[retrievedBlocks.length - 1].l1.blockNumber;
366
- this.log.debug(`Retrieved ${retrievedBlocks.length} new L2 blocks between L1 blocks ${searchStartBlock} and ${searchEndBlock} with last processed L1 block ${lastProcessedL1BlockNumber}.`);
367
- for (const block of retrievedBlocks){
368
- this.log.debug(`Ingesting new L2 block ${block.data.number} with ${block.data.body.txEffects.length} txs`, {
369
- blockHash: block.data.hash(),
370
- l1BlockNumber: block.l1.blockNumber,
371
- ...block.data.header.globalVariables.toInspect(),
372
- ...block.data.getStats()
630
+ this.log.debug(`Retrieved ${retrievedCheckpoints.length} new checkpoints between L1 blocks ${searchStartBlock} and ${searchEndBlock}`, {
631
+ lastProcessedCheckpoint: retrievedCheckpoints[retrievedCheckpoints.length - 1].l1,
632
+ searchStartBlock,
633
+ searchEndBlock
634
+ });
635
+ const publishedCheckpoints = await Promise.all(retrievedCheckpoints.map((b)=>retrievedToPublishedCheckpoint(b)));
636
+ const validCheckpoints = [];
637
+ for (const published of publishedCheckpoints){
638
+ const validationResult = this.config.skipValidateBlockAttestations ? {
639
+ valid: true
640
+ } : await validateCheckpointAttestations(published, this.epochCache, this.l1constants, this.log);
641
+ // Only update the validation result if it has changed, so we can keep track of the first invalid checkpoint
642
+ // in case there is a sequence of more than one invalid checkpoint, as we need to invalidate the first one.
643
+ // There is an exception though: if a checkpoint is invalidated and replaced with another invalid checkpoint,
644
+ // we need to update the validation result, since we need to be able to invalidate the new one.
645
+ // See test 'chain progresses if an invalid checkpoint is invalidated with an invalid one' for more info.
646
+ if (rollupStatus.validationResult?.valid !== validationResult.valid || !rollupStatus.validationResult.valid && !validationResult.valid && rollupStatus.validationResult.block.blockNumber === validationResult.block.blockNumber) {
647
+ rollupStatus.validationResult = validationResult;
648
+ }
649
+ if (!validationResult.valid) {
650
+ this.log.warn(`Skipping checkpoint ${published.checkpoint.number} due to invalid attestations`, {
651
+ checkpointHash: published.checkpoint.hash(),
652
+ l1BlockNumber: published.l1.blockNumber,
653
+ ...pick(validationResult, 'reason')
654
+ });
655
+ // Emit event for invalid block detection
656
+ this.emit(L2BlockSourceEvents.InvalidAttestationsBlockDetected, {
657
+ type: L2BlockSourceEvents.InvalidAttestationsBlockDetected,
658
+ validationResult
659
+ });
660
+ continue;
661
+ }
662
+ validCheckpoints.push(published);
663
+ this.log.debug(`Ingesting new checkpoint ${published.checkpoint.number} with ${published.checkpoint.blocks.length} blocks`, {
664
+ checkpointHash: published.checkpoint.hash(),
665
+ l1BlockNumber: published.l1.blockNumber,
666
+ ...published.checkpoint.header.toInspect(),
667
+ blocks: published.checkpoint.blocks.map((b)=>b.getStats())
373
668
  });
374
669
  }
375
- const [processDuration] = await elapsed(()=>this.store.addBlocks(retrievedBlocks));
376
- this.instrumentation.processNewBlocks(processDuration / retrievedBlocks.length, retrievedBlocks.map((b)=>b.data));
377
- for (const block of retrievedBlocks){
378
- this.log.info(`Downloaded L2 block ${block.data.number}`, {
379
- blockHash: block.data.hash(),
380
- blockNumber: block.data.number,
381
- txCount: block.data.body.txEffects.length,
382
- globalVariables: block.data.header.globalVariables.toInspect()
670
+ try {
671
+ const updatedValidationResult = rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
672
+ const [processDuration] = await elapsed(()=>this.addCheckpoints(validCheckpoints, updatedValidationResult));
673
+ this.instrumentation.processNewBlocks(processDuration / validCheckpoints.length, validCheckpoints.flatMap((c)=>c.checkpoint.blocks));
674
+ } catch (err) {
675
+ if (err instanceof InitialBlockNumberNotSequentialError) {
676
+ const { previousBlockNumber, newBlockNumber } = err;
677
+ const previousBlock = previousBlockNumber ? await this.store.getPublishedBlock(previousBlockNumber) : undefined;
678
+ const updatedL1SyncPoint = previousBlock?.l1.blockNumber ?? this.l1constants.l1StartBlock;
679
+ await this.store.setBlockSynchedL1BlockNumber(updatedL1SyncPoint);
680
+ 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.`, {
681
+ previousBlockNumber,
682
+ previousBlockHash: await previousBlock?.block.hash(),
683
+ newBlockNumber,
684
+ updatedL1SyncPoint
685
+ });
686
+ }
687
+ throw err;
688
+ }
689
+ for (const checkpoint of validCheckpoints){
690
+ this.log.info(`Downloaded checkpoint ${checkpoint.checkpoint.number}`, {
691
+ checkpointHash: checkpoint.checkpoint.hash(),
692
+ checkpointNumber: checkpoint.checkpoint.number,
693
+ blockCount: checkpoint.checkpoint.blocks.length,
694
+ txCount: checkpoint.checkpoint.blocks.reduce((acc, b)=>acc + b.body.txEffects.length, 0),
695
+ header: checkpoint.checkpoint.header.toInspect(),
696
+ archiveRoot: checkpoint.checkpoint.archive.root.toString(),
697
+ archiveNextLeafIndex: checkpoint.checkpoint.archive.nextAvailableLeafIndex
383
698
  });
384
699
  }
700
+ lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
701
+ lastL1BlockWithCheckpoint = publishedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
385
702
  }while (searchEndBlock < currentL1BlockNumber)
386
703
  // Important that we update AFTER inserting the blocks.
387
- await updateProvenBlock();
704
+ await updateProvenCheckpoint();
388
705
  return {
389
- provenBlockNumber
706
+ ...rollupStatus,
707
+ lastRetrievedCheckpoint,
708
+ lastL1BlockWithCheckpoint
390
709
  };
391
710
  }
711
+ async checkForNewCheckpointsBeforeL1SyncPoint(status, blocksSynchedTo, currentL1BlockNumber) {
712
+ const { lastRetrievedCheckpoint, pendingCheckpointNumber } = status;
713
+ // Compare the last checkpoint we have (either retrieved in this round or loaded from store) with what the
714
+ // rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
715
+ const latestLocalCheckpointNumber = lastRetrievedCheckpoint?.checkpoint.number ?? await this.getSynchedCheckpointNumber();
716
+ if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
717
+ // Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
718
+ // but still haven't reached the pending checkpoint according to the call to the rollup contract.
719
+ // We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
720
+ // the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
721
+ // we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
722
+ const latestLocalCheckpoint = lastRetrievedCheckpoint ?? (latestLocalCheckpointNumber > 0 ? await this.getPublishedCheckpoints(latestLocalCheckpointNumber, 1).then(([c])=>c) : undefined);
723
+ const targetL1BlockNumber = latestLocalCheckpoint?.l1.blockNumber ?? maxBigint(currentL1BlockNumber - 64n, 0n);
724
+ const latestLocalCheckpointArchive = latestLocalCheckpoint?.checkpoint.archive.root.toString();
725
+ this.log.warn(`Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` + `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`, {
726
+ latestLocalCheckpointNumber,
727
+ latestLocalCheckpointArchive,
728
+ blocksSynchedTo,
729
+ currentL1BlockNumber,
730
+ ...status
731
+ });
732
+ await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
733
+ } else {
734
+ this.log.trace(`No new checkpoints behind L1 sync point to retrieve.`, {
735
+ latestLocalCheckpointNumber,
736
+ pendingCheckpointNumber
737
+ });
738
+ }
739
+ }
740
+ /** Resumes the archiver after a stop. */ resume() {
741
+ if (this.runningPromise.isRunning()) {
742
+ this.log.warn(`Archiver already running`);
743
+ }
744
+ this.log.info(`Restarting archiver`);
745
+ this.runningPromise.start();
746
+ }
392
747
  /**
393
748
  * Stops the archiver.
394
749
  * @returns A promise signalling completion of the stop process.
395
750
  */ async stop() {
396
751
  this.log.debug('Stopping...');
397
- await this.runningPromise?.stop();
752
+ await this.runningPromise.stop();
398
753
  this.log.info('Stopped.');
399
754
  return Promise.resolve();
400
755
  }
756
+ backupTo(destPath) {
757
+ return this.dataStore.backupTo(destPath);
758
+ }
401
759
  getL1Constants() {
402
760
  return Promise.resolve(this.l1constants);
403
761
  }
762
+ getGenesisValues() {
763
+ return Promise.resolve({
764
+ genesisArchiveRoot: this.l1constants.genesisArchiveRoot
765
+ });
766
+ }
404
767
  getRollupAddress() {
405
768
  return Promise.resolve(this.l1Addresses.rollupAddress);
406
769
  }
@@ -408,24 +771,16 @@ import { ArchiverInstrumentation } from './instrumentation.js';
408
771
  return Promise.resolve(this.l1Addresses.registryAddress);
409
772
  }
410
773
  getL1BlockNumber() {
411
- const l1BlockNumber = this.l1BlockNumber;
412
- if (!l1BlockNumber) {
413
- throw new Error('L1 block number not yet available. Complete an initial sync first.');
414
- }
415
- return l1BlockNumber;
774
+ return this.l1BlockNumber;
416
775
  }
417
776
  getL1Timestamp() {
418
- const l1Timestamp = this.l1Timestamp;
419
- if (!l1Timestamp) {
420
- throw new Error('L1 timestamp not yet available. Complete an initial sync first.');
421
- }
422
- return l1Timestamp;
777
+ return Promise.resolve(this.l1Timestamp);
423
778
  }
424
779
  getL2SlotNumber() {
425
- return Promise.resolve(getSlotAtTimestamp(this.getL1Timestamp(), this.l1constants));
780
+ return Promise.resolve(this.l1Timestamp === undefined ? undefined : getSlotAtTimestamp(this.l1Timestamp, this.l1constants));
426
781
  }
427
782
  getL2EpochNumber() {
428
- return Promise.resolve(getEpochNumberAtTimestamp(this.getL1Timestamp(), this.l1constants));
783
+ return Promise.resolve(this.l1Timestamp === undefined ? undefined : getEpochNumberAtTimestamp(this.l1Timestamp, this.l1constants));
429
784
  }
430
785
  async getBlocksForEpoch(epochNumber) {
431
786
  const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
@@ -433,7 +788,7 @@ import { ArchiverInstrumentation } from './instrumentation.js';
433
788
  // Walk the list of blocks backwards and filter by slots matching the requested epoch.
434
789
  // We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
435
790
  let block = await this.getBlock(await this.store.getSynchedL2BlockNumber());
436
- const slot = (b)=>b.header.globalVariables.slotNumber.toBigInt();
791
+ const slot = (b)=>b.header.globalVariables.slotNumber;
437
792
  while(block && slot(block) >= start){
438
793
  if (slot(block) <= end) {
439
794
  blocks.push(block);
@@ -442,10 +797,26 @@ import { ArchiverInstrumentation } from './instrumentation.js';
442
797
  }
443
798
  return blocks.reverse();
444
799
  }
800
+ async getBlockHeadersForEpoch(epochNumber) {
801
+ const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
802
+ const blocks = [];
803
+ // Walk the list of blocks backwards and filter by slots matching the requested epoch.
804
+ // We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
805
+ let number = await this.store.getSynchedL2BlockNumber();
806
+ let header = await this.getBlockHeader(number);
807
+ const slot = (b)=>b.globalVariables.slotNumber;
808
+ while(header && slot(header) >= start){
809
+ if (slot(header) <= end) {
810
+ blocks.push(header);
811
+ }
812
+ header = await this.getBlockHeader(--number);
813
+ }
814
+ return blocks.reverse();
815
+ }
445
816
  async isEpochComplete(epochNumber) {
446
817
  // The epoch is complete if the current L2 block is the last one in the epoch (or later)
447
818
  const header = await this.getBlockHeader('latest');
448
- const slot = header?.globalVariables.slotNumber.toBigInt();
819
+ const slot = header ? header.globalVariables.slotNumber : undefined;
449
820
  const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1constants);
450
821
  if (slot && slot >= endSlot) {
451
822
  return true;
@@ -466,15 +837,87 @@ import { ArchiverInstrumentation } from './instrumentation.js';
466
837
  const leeway = 1n;
467
838
  return l1Timestamp + leeway >= endTimestamp;
468
839
  }
840
+ /** Returns whether the archiver has completed an initial sync run successfully. */ isInitialSyncComplete() {
841
+ return this.initialSyncComplete;
842
+ }
843
+ async getPublishedCheckpoints(from, limit, proven) {
844
+ const blocks = await this.getPublishedBlocks(from, limit, proven);
845
+ return blocks.map((b)=>b.toPublishedCheckpoint());
846
+ }
847
+ async getCheckpoints(from, limit, proven) {
848
+ const published = await this.getPublishedCheckpoints(from, limit, proven);
849
+ return published.map((p)=>p.checkpoint);
850
+ }
851
+ async getCheckpoint(number) {
852
+ if (number < 0) {
853
+ number = await this.getSynchedCheckpointNumber();
854
+ }
855
+ if (number === 0) {
856
+ return undefined;
857
+ }
858
+ const published = await this.getPublishedCheckpoints(number, 1);
859
+ return published[0]?.checkpoint;
860
+ }
861
+ async getCheckpointHeader(number) {
862
+ if (number === 'latest') {
863
+ number = await this.getSynchedCheckpointNumber();
864
+ }
865
+ if (number === 0) {
866
+ return undefined;
867
+ }
868
+ const checkpoint = await this.getCheckpoint(number);
869
+ return checkpoint?.header;
870
+ }
871
+ getCheckpointNumber() {
872
+ return this.getSynchedCheckpointNumber();
873
+ }
874
+ getSynchedCheckpointNumber() {
875
+ // TODO: Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
876
+ return this.store.getSynchedL2BlockNumber();
877
+ }
878
+ getProvenCheckpointNumber() {
879
+ // TODO: Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
880
+ return this.store.getProvenL2BlockNumber();
881
+ }
882
+ setProvenCheckpointNumber(checkpointNumber) {
883
+ // TODO: Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
884
+ return this.store.setProvenL2BlockNumber(checkpointNumber);
885
+ }
886
+ unwindCheckpoints(from, checkpointsToUnwind) {
887
+ // TODO: This only works if we have one block per checkpoint.
888
+ return this.store.unwindBlocks(from, checkpointsToUnwind);
889
+ }
890
+ getLastBlockNumberInCheckpoint(checkpointNumber) {
891
+ // TODO: Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
892
+ return Promise.resolve(checkpointNumber);
893
+ }
894
+ addCheckpoints(checkpoints, pendingChainValidationStatus) {
895
+ return this.store.addBlocks(checkpoints.map((p)=>PublishedL2Block.fromPublishedCheckpoint(p)), pendingChainValidationStatus);
896
+ }
469
897
  /**
470
898
  * Gets up to `limit` amount of L2 blocks starting from `from`.
471
899
  * @param from - Number of the first block to return (inclusive).
472
900
  * @param limit - The number of blocks to return.
473
901
  * @param proven - If true, only return blocks that have been proven.
474
902
  * @returns The requested L2 blocks.
475
- */ async getBlocks(from, limit, proven) {
903
+ */ getBlocks(from, limit, proven) {
904
+ return this.getPublishedBlocks(from, limit, proven).then((blocks)=>blocks.map((b)=>b.block));
905
+ }
906
+ /** Equivalent to getBlocks but includes publish data. */ async getPublishedBlocks(from, limit, proven) {
476
907
  const limitWithProven = proven ? Math.min(limit, Math.max(await this.store.getProvenL2BlockNumber() - from + 1, 0)) : limit;
477
- return limitWithProven === 0 ? [] : (await this.store.getBlocks(from, limitWithProven)).map((b)=>b.data);
908
+ return limitWithProven === 0 ? [] : await this.store.getPublishedBlocks(from, limitWithProven);
909
+ }
910
+ getPublishedBlockByHash(blockHash) {
911
+ return this.store.getPublishedBlockByHash(blockHash);
912
+ }
913
+ getPublishedBlockByArchive(archive) {
914
+ return this.store.getPublishedBlockByArchive(archive);
915
+ }
916
+ getBlockHeaderByHash(blockHash) {
917
+ return this.store.getBlockHeaderByHash(blockHash);
918
+ }
919
+ getBlockHeaderByArchive(archive) {
920
+ return this.store.getBlockHeaderByArchive(archive);
478
921
  }
479
922
  /**
480
923
  * Gets an l2 block.
@@ -485,11 +928,11 @@ import { ArchiverInstrumentation } from './instrumentation.js';
485
928
  if (number < 0) {
486
929
  number = await this.store.getSynchedL2BlockNumber();
487
930
  }
488
- if (number == 0) {
931
+ if (number === 0) {
489
932
  return undefined;
490
933
  }
491
- const blocks = await this.store.getBlocks(number, 1);
492
- return blocks.length === 0 ? undefined : blocks[0].data;
934
+ const publishedBlock = await this.store.getPublishedBlock(number);
935
+ return publishedBlock?.block;
493
936
  }
494
937
  async getBlockHeader(number) {
495
938
  if (number === 'latest') {
@@ -508,22 +951,6 @@ import { ArchiverInstrumentation } from './instrumentation.js';
508
951
  return this.store.getSettledTxReceipt(txHash);
509
952
  }
510
953
  /**
511
- * Gets the public function data for a contract.
512
- * @param address - The contract address containing the function to fetch.
513
- * @param selector - The function selector of the function to fetch.
514
- * @returns The public function data (if found).
515
- */ async getPublicFunction(address, selector) {
516
- const instance = await this.getContract(address);
517
- if (!instance) {
518
- throw new Error(`Contract ${address.toString()} not found`);
519
- }
520
- const contractClass = await this.getContractClass(instance.currentContractClassId);
521
- if (!contractClass) {
522
- throw new Error(`Contract class ${instance.currentContractClassId.toString()} for ${address.toString()} not found`);
523
- }
524
- return contractClass.publicFunctions.find((f)=>f.selector.equals(selector));
525
- }
526
- /**
527
954
  * Retrieves all private logs from up to `limit` blocks, starting from the block number `from`.
528
955
  * @param from - The block number from which to begin retrieving logs.
529
956
  * @param limit - The maximum number of blocks to retrieve logs from.
@@ -540,15 +967,6 @@ import { ArchiverInstrumentation } from './instrumentation.js';
540
967
  return this.store.getLogsByTags(tags);
541
968
  }
542
969
  /**
543
- * Returns the provided nullifier indexes scoped to the block
544
- * they were first included in, or undefined if they're not present in the tree
545
- * @param blockNumber Max block number to search for the nullifiers
546
- * @param nullifiers Nullifiers to get
547
- * @returns The block scoped indexes of the provided nullifiers, or undefined if the nullifier doesn't exist in the tree
548
- */ findNullifiersIndexesWithBlock(blockNumber, nullifiers) {
549
- return this.store.findNullifiersIndexesWithBlock(blockNumber, nullifiers);
550
- }
551
- /**
552
970
  * Gets public logs based on the provided filter.
553
971
  * @param filter - The filter to apply to the logs.
554
972
  * @returns The requested logs.
@@ -580,8 +998,16 @@ import { ArchiverInstrumentation } from './instrumentation.js';
580
998
  getBytecodeCommitment(id) {
581
999
  return this.store.getBytecodeCommitment(id);
582
1000
  }
583
- getContract(address) {
584
- return this.store.getContractInstance(address);
1001
+ async getContract(address, maybeTimestamp) {
1002
+ let timestamp;
1003
+ if (maybeTimestamp === undefined) {
1004
+ const latestBlockHeader = await this.getBlockHeader('latest');
1005
+ // If we get undefined block header, it means that the archiver has not yet synced any block so we default to 0.
1006
+ timestamp = latestBlockHeader ? latestBlockHeader.globalVariables.timestamp : 0n;
1007
+ } else {
1008
+ timestamp = maybeTimestamp;
1009
+ }
1010
+ return this.store.getContractInstance(address, timestamp);
585
1011
  }
586
1012
  /**
587
1013
  * Gets L1 to L2 message (to be) included in a given block.
@@ -600,29 +1026,33 @@ import { ArchiverInstrumentation } from './instrumentation.js';
600
1026
  getContractClassIds() {
601
1027
  return this.store.getContractClassIds();
602
1028
  }
603
- // TODO(#10007): Remove this method
604
- async addContractClass(contractClass) {
605
- await this.store.addContractClasses([
606
- contractClass
607
- ], [
608
- await computePublicBytecodeCommitment(contractClass.packedBytecode)
609
- ], 0);
610
- return;
1029
+ registerContractFunctionSignatures(signatures) {
1030
+ return this.store.registerContractFunctionSignatures(signatures);
611
1031
  }
612
- registerContractFunctionSignatures(address, signatures) {
613
- return this.store.registerContractFunctionSignatures(address, signatures);
1032
+ getDebugFunctionName(address, selector) {
1033
+ return this.store.getDebugFunctionName(address, selector);
614
1034
  }
615
- getContractFunctionName(address, selector) {
616
- return this.store.getContractFunctionName(address, selector);
1035
+ async getPendingChainValidationStatus() {
1036
+ return await this.store.getPendingChainValidationStatus() ?? {
1037
+ valid: true
1038
+ };
1039
+ }
1040
+ isPendingChainInvalid() {
1041
+ return this.getPendingChainValidationStatus().then((status)=>!status.valid);
617
1042
  }
618
1043
  async getL2Tips() {
619
1044
  const [latestBlockNumber, provenBlockNumber] = await Promise.all([
620
1045
  this.getBlockNumber(),
621
1046
  this.getProvenBlockNumber()
622
1047
  ]);
623
- const [latestBlockHeader, provenBlockHeader] = await Promise.all([
1048
+ // TODO(#13569): Compute proper finalized block number based on L1 finalized block.
1049
+ // We just force it 2 epochs worth of proven data for now.
1050
+ // NOTE: update end-to-end/src/e2e_epochs/epochs_empty_blocks.test.ts as that uses finalized blocks in computations
1051
+ const finalizedBlockNumber = Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0);
1052
+ const [latestBlockHeader, provenBlockHeader, finalizedBlockHeader] = await Promise.all([
624
1053
  latestBlockNumber > 0 ? this.getBlockHeader(latestBlockNumber) : undefined,
625
- provenBlockNumber > 0 ? this.getBlockHeader(provenBlockNumber) : undefined
1054
+ provenBlockNumber > 0 ? this.getBlockHeader(provenBlockNumber) : undefined,
1055
+ finalizedBlockNumber > 0 ? this.getBlockHeader(finalizedBlockNumber) : undefined
626
1056
  ]);
627
1057
  if (latestBlockNumber > 0 && !latestBlockHeader) {
628
1058
  throw new Error(`Failed to retrieve latest block header for block ${latestBlockNumber}`);
@@ -630,9 +1060,12 @@ import { ArchiverInstrumentation } from './instrumentation.js';
630
1060
  if (provenBlockNumber > 0 && !provenBlockHeader) {
631
1061
  throw new Error(`Failed to retrieve proven block header for block ${provenBlockNumber} (latest block is ${latestBlockNumber})`);
632
1062
  }
1063
+ if (finalizedBlockNumber > 0 && !finalizedBlockHeader) {
1064
+ throw new Error(`Failed to retrieve finalized block header for block ${finalizedBlockNumber} (latest block is ${latestBlockNumber})`);
1065
+ }
633
1066
  const latestBlockHeaderHash = await latestBlockHeader?.hash();
634
1067
  const provenBlockHeaderHash = await provenBlockHeader?.hash();
635
- const finalizedBlockHeaderHash = await provenBlockHeader?.hash();
1068
+ const finalizedBlockHeaderHash = await finalizedBlockHeader?.hash();
636
1069
  return {
637
1070
  latest: {
638
1071
  number: latestBlockNumber,
@@ -643,16 +1076,49 @@ import { ArchiverInstrumentation } from './instrumentation.js';
643
1076
  hash: provenBlockHeaderHash?.toString()
644
1077
  },
645
1078
  finalized: {
646
- number: provenBlockNumber,
1079
+ number: finalizedBlockNumber,
647
1080
  hash: finalizedBlockHeaderHash?.toString()
648
1081
  }
649
1082
  };
650
1083
  }
1084
+ async rollbackTo(targetL2BlockNumber) {
1085
+ const currentBlocks = await this.getL2Tips();
1086
+ const currentL2Block = currentBlocks.latest.number;
1087
+ const currentProvenBlock = currentBlocks.proven.number;
1088
+ // const currentFinalizedBlock = currentBlocks.finalized.number;
1089
+ if (targetL2BlockNumber >= currentL2Block) {
1090
+ throw new Error(`Target L2 block ${targetL2BlockNumber} must be less than current L2 block ${currentL2Block}`);
1091
+ }
1092
+ const blocksToUnwind = currentL2Block - targetL2BlockNumber;
1093
+ const targetL2Block = await this.store.getPublishedBlock(targetL2BlockNumber);
1094
+ if (!targetL2Block) {
1095
+ throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
1096
+ }
1097
+ const targetL1BlockNumber = targetL2Block.l1.blockNumber;
1098
+ const targetL1BlockHash = await this.getL1BlockHash(targetL1BlockNumber);
1099
+ this.log.info(`Unwinding ${blocksToUnwind} blocks from L2 block ${currentL2Block}`);
1100
+ await this.store.unwindBlocks(currentL2Block, blocksToUnwind);
1101
+ this.log.info(`Unwinding L1 to L2 messages to ${targetL2BlockNumber}`);
1102
+ await this.store.rollbackL1ToL2MessagesToL2Block(targetL2BlockNumber);
1103
+ this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
1104
+ await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
1105
+ await this.store.setMessageSynchedL1Block({
1106
+ l1BlockNumber: targetL1BlockNumber,
1107
+ l1BlockHash: targetL1BlockHash
1108
+ });
1109
+ if (targetL2BlockNumber < currentProvenBlock) {
1110
+ this.log.info(`Clearing proven L2 block number`);
1111
+ await this.store.setProvenL2BlockNumber(0);
1112
+ }
1113
+ // TODO(palla/reorg): Set the finalized block when we add support for it.
1114
+ // if (targetL2BlockNumber < currentFinalizedBlock) {
1115
+ // this.log.info(`Clearing finalized L2 block number`);
1116
+ // await this.store.setFinalizedL2BlockNumber(0);
1117
+ // }
1118
+ }
651
1119
  }
652
1120
  _ts_decorate([
653
- trackSpan('Archiver.sync', (initialRun)=>({
654
- [Attributes.INITIAL_SYNC]: initialRun
655
- }))
1121
+ trackSpan('Archiver.sync')
656
1122
  ], Archiver.prototype, "sync", null);
657
1123
  var Operation = /*#__PURE__*/ function(Operation) {
658
1124
  Operation[Operation["Store"] = 0] = "Store";
@@ -664,23 +1130,19 @@ var Operation = /*#__PURE__*/ function(Operation) {
664
1130
  *
665
1131
  * I would have preferred to not have this type. But it is useful for handling the logic that any
666
1132
  * store would need to include otherwise while exposing fewer functions and logic directly to the archiver.
667
- */ class ArchiverStoreHelper {
1133
+ */ export class ArchiverStoreHelper {
668
1134
  store;
669
1135
  #log;
670
1136
  constructor(store){
671
1137
  this.store = store;
672
1138
  this.#log = createLogger('archiver:block-helper');
673
1139
  }
674
- // TODO(#10007): Remove this method
675
- addContractClasses(contractClasses, bytecodeCommitments, blockNum) {
676
- return this.store.addContractClasses(contractClasses, bytecodeCommitments, blockNum);
677
- }
678
1140
  /**
679
- * Extracts and stores contract classes out of ContractClassRegistered events emitted by the class registerer contract.
1141
+ * Extracts and stores contract classes out of ContractClassPublished events emitted by the class registry contract.
680
1142
  * @param allLogs - All logs emitted in a bunch of blocks.
681
- */ async #updateRegisteredContractClasses(allLogs, blockNum, operation) {
682
- const contractClassRegisteredEvents = allLogs.filter((log)=>ContractClassRegisteredEvent.isContractClassRegisteredEvent(log)).map((log)=>ContractClassRegisteredEvent.fromLog(log));
683
- const contractClasses = await Promise.all(contractClassRegisteredEvents.map((e)=>e.toContractClassPublic()));
1143
+ */ async #updatePublishedContractClasses(allLogs, blockNum, operation) {
1144
+ const contractClassPublishedEvents = allLogs.filter((log)=>ContractClassPublishedEvent.isContractClassPublishedEvent(log)).map((log)=>ContractClassPublishedEvent.fromLog(log));
1145
+ const contractClasses = await Promise.all(contractClassPublishedEvents.map((e)=>e.toContractClassPublic()));
684
1146
  if (contractClasses.length > 0) {
685
1147
  contractClasses.forEach((c)=>this.#log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
686
1148
  if (operation == 0) {
@@ -694,10 +1156,10 @@ var Operation = /*#__PURE__*/ function(Operation) {
694
1156
  return true;
695
1157
  }
696
1158
  /**
697
- * Extracts and stores contract instances out of ContractInstanceDeployed events emitted by the canonical deployer contract.
1159
+ * Extracts and stores contract instances out of ContractInstancePublished events emitted by the canonical deployer contract.
698
1160
  * @param allLogs - All logs emitted in a bunch of blocks.
699
1161
  */ async #updateDeployedContractInstances(allLogs, blockNum, operation) {
700
- const contractInstances = allLogs.filter((log)=>ContractInstanceDeployedEvent.isContractInstanceDeployedEvent(log)).map((log)=>ContractInstanceDeployedEvent.fromLog(log)).map((e)=>e.toContractInstance());
1162
+ const contractInstances = allLogs.filter((log)=>ContractInstancePublishedEvent.isContractInstancePublishedEvent(log)).map((log)=>ContractInstancePublishedEvent.fromLog(log)).map((e)=>e.toContractInstance());
701
1163
  if (contractInstances.length > 0) {
702
1164
  contractInstances.forEach((c)=>this.#log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`));
703
1165
  if (operation == 0) {
@@ -709,22 +1171,24 @@ var Operation = /*#__PURE__*/ function(Operation) {
709
1171
  return true;
710
1172
  }
711
1173
  /**
712
- * Extracts and stores contract instances out of ContractInstanceDeployed events emitted by the canonical deployer contract.
1174
+ * Extracts and stores contract instances out of ContractInstancePublished events emitted by the canonical deployer contract.
713
1175
  * @param allLogs - All logs emitted in a bunch of blocks.
714
- */ async #updateUpdatedContractInstances(allLogs, blockNum, operation) {
1176
+ * @param timestamp - Timestamp at which the updates were scheduled.
1177
+ * @param operation - The operation to perform on the contract instance updates (Store or Delete).
1178
+ */ async #updateUpdatedContractInstances(allLogs, timestamp, operation) {
715
1179
  const contractUpdates = allLogs.filter((log)=>ContractInstanceUpdatedEvent.isContractInstanceUpdatedEvent(log)).map((log)=>ContractInstanceUpdatedEvent.fromLog(log)).map((e)=>e.toContractInstanceUpdate());
716
1180
  if (contractUpdates.length > 0) {
717
1181
  contractUpdates.forEach((c)=>this.#log.verbose(`${Operation[operation]} contract instance update at ${c.address.toString()}`));
718
1182
  if (operation == 0) {
719
- return await this.store.addContractInstanceUpdates(contractUpdates, blockNum);
1183
+ return await this.store.addContractInstanceUpdates(contractUpdates, timestamp);
720
1184
  } else if (operation == 1) {
721
- return await this.store.deleteContractInstanceUpdates(contractUpdates, blockNum);
1185
+ return await this.store.deleteContractInstanceUpdates(contractUpdates, timestamp);
722
1186
  }
723
1187
  }
724
1188
  return true;
725
1189
  }
726
1190
  /**
727
- * Stores the functions that was broadcasted individually
1191
+ * Stores the functions that were broadcasted individually
728
1192
  *
729
1193
  * @dev Beware that there is not a delete variant of this, since they are added to contract classes
730
1194
  * and will be deleted as part of the class if needed.
@@ -733,13 +1197,13 @@ var Operation = /*#__PURE__*/ function(Operation) {
733
1197
  * @param _blockNum - The block number
734
1198
  * @returns
735
1199
  */ async #storeBroadcastedIndividualFunctions(allLogs, _blockNum) {
736
- // Filter out private and unconstrained function broadcast events
1200
+ // Filter out private and utility function broadcast events
737
1201
  const privateFnEvents = allLogs.filter((log)=>PrivateFunctionBroadcastedEvent.isPrivateFunctionBroadcastedEvent(log)).map((log)=>PrivateFunctionBroadcastedEvent.fromLog(log));
738
- const unconstrainedFnEvents = allLogs.filter((log)=>UnconstrainedFunctionBroadcastedEvent.isUnconstrainedFunctionBroadcastedEvent(log)).map((log)=>UnconstrainedFunctionBroadcastedEvent.fromLog(log));
1202
+ const utilityFnEvents = allLogs.filter((log)=>UtilityFunctionBroadcastedEvent.isUtilityFunctionBroadcastedEvent(log)).map((log)=>UtilityFunctionBroadcastedEvent.fromLog(log));
739
1203
  // Group all events by contract class id
740
1204
  for (const [classIdString, classEvents] of Object.entries(groupBy([
741
1205
  ...privateFnEvents,
742
- ...unconstrainedFnEvents
1206
+ ...utilityFnEvents
743
1207
  ], (e)=>e.contractClassId.toString()))){
744
1208
  const contractClassId = Fr.fromHexString(classIdString);
745
1209
  const contractClass = await this.getContractClass(contractClassId);
@@ -747,21 +1211,21 @@ var Operation = /*#__PURE__*/ function(Operation) {
747
1211
  this.#log.warn(`Skipping broadcasted functions as contract class ${contractClassId.toString()} was not found`);
748
1212
  continue;
749
1213
  }
750
- // Split private and unconstrained functions, and filter out invalid ones
1214
+ // Split private and utility functions, and filter out invalid ones
751
1215
  const allFns = classEvents.map((e)=>e.toFunctionWithMembershipProof());
752
- const privateFns = allFns.filter((fn)=>'unconstrainedFunctionsArtifactTreeRoot' in fn);
753
- const unconstrainedFns = allFns.filter((fn)=>'privateFunctionsArtifactTreeRoot' in fn);
1216
+ const privateFns = allFns.filter((fn)=>'utilityFunctionsTreeRoot' in fn);
1217
+ const utilityFns = allFns.filter((fn)=>'privateFunctionsArtifactTreeRoot' in fn);
754
1218
  const privateFunctionsWithValidity = await Promise.all(privateFns.map(async (fn)=>({
755
1219
  fn,
756
1220
  valid: await isValidPrivateFunctionMembershipProof(fn, contractClass)
757
1221
  })));
758
1222
  const validPrivateFns = privateFunctionsWithValidity.filter(({ valid })=>valid).map(({ fn })=>fn);
759
- const unconstrainedFunctionsWithValidity = await Promise.all(unconstrainedFns.map(async (fn)=>({
1223
+ const utilityFunctionsWithValidity = await Promise.all(utilityFns.map(async (fn)=>({
760
1224
  fn,
761
- valid: await isValidUnconstrainedFunctionMembershipProof(fn, contractClass)
1225
+ valid: await isValidUtilityFunctionMembershipProof(fn, contractClass)
762
1226
  })));
763
- const validUnconstrainedFns = unconstrainedFunctionsWithValidity.filter(({ valid })=>valid).map(({ fn })=>fn);
764
- const validFnCount = validPrivateFns.length + validUnconstrainedFns.length;
1227
+ const validUtilityFns = utilityFunctionsWithValidity.filter(({ valid })=>valid).map(({ fn })=>fn);
1228
+ const validFnCount = validPrivateFns.length + validUtilityFns.length;
765
1229
  if (validFnCount !== allFns.length) {
766
1230
  this.#log.warn(`Skipping ${allFns.length - validFnCount} invalid functions`);
767
1231
  }
@@ -769,62 +1233,90 @@ var Operation = /*#__PURE__*/ function(Operation) {
769
1233
  if (validFnCount > 0) {
770
1234
  this.#log.verbose(`Storing ${validFnCount} functions for contract class ${contractClassId.toString()}`);
771
1235
  }
772
- return await this.store.addFunctions(contractClassId, validPrivateFns, validUnconstrainedFns);
1236
+ return await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
773
1237
  }
774
1238
  return true;
775
1239
  }
776
- async addBlocks(blocks) {
777
- const opResults = await Promise.all([
778
- this.store.addLogs(blocks.map((block)=>block.data)),
779
- // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
780
- ...blocks.map(async (block)=>{
781
- const contractClassLogs = block.data.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
782
- // ContractInstanceDeployed event logs are broadcast in privateLogs.
783
- const privateLogs = block.data.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
784
- const publicLogs = block.data.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
785
- return (await Promise.all([
786
- this.#updateRegisteredContractClasses(contractClassLogs, block.data.number, 0),
787
- this.#updateDeployedContractInstances(privateLogs, block.data.number, 0),
788
- this.#updateUpdatedContractInstances(publicLogs, block.data.number, 0),
789
- this.#storeBroadcastedIndividualFunctions(contractClassLogs, block.data.number)
790
- ])).every(Boolean);
791
- }),
792
- this.store.addNullifiers(blocks.map((block)=>block.data)),
793
- this.store.addBlocks(blocks)
794
- ]);
795
- return opResults.every(Boolean);
1240
+ addBlocks(blocks, pendingChainValidationStatus) {
1241
+ // Add the blocks to the store. Store will throw if the blocks are not in order, there are gaps,
1242
+ // or if the previous block is not in the store.
1243
+ return this.store.transactionAsync(async ()=>{
1244
+ await this.store.addBlocks(blocks);
1245
+ const opResults = await Promise.all([
1246
+ // Update the pending chain validation status if provided
1247
+ pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
1248
+ // Add any logs emitted during the retrieved blocks
1249
+ this.store.addLogs(blocks.map((block)=>block.block)),
1250
+ // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
1251
+ ...blocks.map(async (block)=>{
1252
+ const contractClassLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
1253
+ // ContractInstancePublished event logs are broadcast in privateLogs.
1254
+ const privateLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
1255
+ const publicLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
1256
+ return (await Promise.all([
1257
+ this.#updatePublishedContractClasses(contractClassLogs, block.block.number, 0),
1258
+ this.#updateDeployedContractInstances(privateLogs, block.block.number, 0),
1259
+ this.#updateUpdatedContractInstances(publicLogs, block.block.header.globalVariables.timestamp, 0),
1260
+ this.#storeBroadcastedIndividualFunctions(contractClassLogs, block.block.number)
1261
+ ])).every(Boolean);
1262
+ })
1263
+ ]);
1264
+ return opResults.every(Boolean);
1265
+ });
796
1266
  }
797
1267
  async unwindBlocks(from, blocksToUnwind) {
798
1268
  const last = await this.getSynchedL2BlockNumber();
799
1269
  if (from != last) {
800
- throw new Error(`Can only remove from the tip`);
1270
+ throw new Error(`Cannot unwind blocks from block ${from} when the last block is ${last}`);
1271
+ }
1272
+ if (blocksToUnwind <= 0) {
1273
+ throw new Error(`Cannot unwind ${blocksToUnwind} blocks`);
801
1274
  }
802
1275
  // from - blocksToUnwind = the new head, so + 1 for what we need to remove
803
- const blocks = await this.getBlocks(from - blocksToUnwind + 1, blocksToUnwind);
1276
+ const blocks = await this.getPublishedBlocks(from - blocksToUnwind + 1, blocksToUnwind);
804
1277
  const opResults = await Promise.all([
1278
+ // Prune rolls back to the last proven block, which is by definition valid
1279
+ this.store.setPendingChainValidationStatus({
1280
+ valid: true
1281
+ }),
805
1282
  // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
806
1283
  ...blocks.map(async (block)=>{
807
- const contractClassLogs = block.data.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
808
- // ContractInstanceDeployed event logs are broadcast in privateLogs.
809
- const privateLogs = block.data.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
810
- const publicLogs = block.data.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
1284
+ const contractClassLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
1285
+ // ContractInstancePublished event logs are broadcast in privateLogs.
1286
+ const privateLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
1287
+ const publicLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
811
1288
  return (await Promise.all([
812
- this.#updateRegisteredContractClasses(contractClassLogs, block.data.number, 1),
813
- this.#updateDeployedContractInstances(privateLogs, block.data.number, 1),
814
- this.#updateUpdatedContractInstances(publicLogs, block.data.number, 1)
1289
+ this.#updatePublishedContractClasses(contractClassLogs, block.block.number, 1),
1290
+ this.#updateDeployedContractInstances(privateLogs, block.block.number, 1),
1291
+ this.#updateUpdatedContractInstances(publicLogs, block.block.header.globalVariables.timestamp, 1)
815
1292
  ])).every(Boolean);
816
1293
  }),
817
- this.store.deleteLogs(blocks.map((b)=>b.data)),
1294
+ this.store.deleteLogs(blocks.map((b)=>b.block)),
818
1295
  this.store.unwindBlocks(from, blocksToUnwind)
819
1296
  ]);
820
1297
  return opResults.every(Boolean);
821
1298
  }
822
- getBlocks(from, limit) {
823
- return this.store.getBlocks(from, limit);
1299
+ getPublishedBlocks(from, limit) {
1300
+ return this.store.getPublishedBlocks(from, limit);
1301
+ }
1302
+ getPublishedBlock(number) {
1303
+ return this.store.getPublishedBlock(number);
1304
+ }
1305
+ getPublishedBlockByHash(blockHash) {
1306
+ return this.store.getPublishedBlockByHash(blockHash);
1307
+ }
1308
+ getPublishedBlockByArchive(archive) {
1309
+ return this.store.getPublishedBlockByArchive(archive);
824
1310
  }
825
1311
  getBlockHeaders(from, limit) {
826
1312
  return this.store.getBlockHeaders(from, limit);
827
1313
  }
1314
+ getBlockHeaderByHash(blockHash) {
1315
+ return this.store.getBlockHeaderByHash(blockHash);
1316
+ }
1317
+ getBlockHeaderByArchive(archive) {
1318
+ return this.store.getBlockHeaderByArchive(archive);
1319
+ }
828
1320
  getTxEffect(txHash) {
829
1321
  return this.store.getTxEffect(txHash);
830
1322
  }
@@ -843,11 +1335,8 @@ var Operation = /*#__PURE__*/ function(Operation) {
843
1335
  getPrivateLogs(from, limit) {
844
1336
  return this.store.getPrivateLogs(from, limit);
845
1337
  }
846
- getLogsByTags(tags) {
847
- return this.store.getLogsByTags(tags);
848
- }
849
- findNullifiersIndexesWithBlock(blockNumber, nullifiers) {
850
- return this.store.findNullifiersIndexesWithBlock(blockNumber, nullifiers);
1338
+ getLogsByTags(tags, logsPerTag) {
1339
+ return this.store.getLogsByTags(tags, logsPerTag);
851
1340
  }
852
1341
  getPublicLogs(filter) {
853
1342
  return this.store.getPublicLogs(filter);
@@ -867,8 +1356,8 @@ var Operation = /*#__PURE__*/ function(Operation) {
867
1356
  setBlockSynchedL1BlockNumber(l1BlockNumber) {
868
1357
  return this.store.setBlockSynchedL1BlockNumber(l1BlockNumber);
869
1358
  }
870
- setMessageSynchedL1BlockNumber(l1BlockNumber) {
871
- return this.store.setMessageSynchedL1BlockNumber(l1BlockNumber);
1359
+ setMessageSynchedL1Block(l1Block) {
1360
+ return this.store.setMessageSynchedL1Block(l1Block);
872
1361
  }
873
1362
  getSynchPoint() {
874
1363
  return this.store.getSynchPoint();
@@ -879,17 +1368,17 @@ var Operation = /*#__PURE__*/ function(Operation) {
879
1368
  getBytecodeCommitment(contractClassId) {
880
1369
  return this.store.getBytecodeCommitment(contractClassId);
881
1370
  }
882
- getContractInstance(address) {
883
- return this.store.getContractInstance(address);
1371
+ getContractInstance(address, timestamp) {
1372
+ return this.store.getContractInstance(address, timestamp);
884
1373
  }
885
1374
  getContractClassIds() {
886
1375
  return this.store.getContractClassIds();
887
1376
  }
888
- registerContractFunctionSignatures(address, signatures) {
889
- return this.store.registerContractFunctionSignatures(address, signatures);
1377
+ registerContractFunctionSignatures(signatures) {
1378
+ return this.store.registerContractFunctionSignatures(signatures);
890
1379
  }
891
- getContractFunctionName(address, selector) {
892
- return this.store.getContractFunctionName(address, selector);
1380
+ getDebugFunctionName(address, selector) {
1381
+ return this.store.getDebugFunctionName(address, selector);
893
1382
  }
894
1383
  getTotalL1ToL2MessageCount() {
895
1384
  return this.store.getTotalL1ToL2MessageCount();
@@ -897,4 +1386,23 @@ var Operation = /*#__PURE__*/ function(Operation) {
897
1386
  estimateSize() {
898
1387
  return this.store.estimateSize();
899
1388
  }
1389
+ rollbackL1ToL2MessagesToL2Block(targetBlockNumber) {
1390
+ return this.store.rollbackL1ToL2MessagesToL2Block(targetBlockNumber);
1391
+ }
1392
+ iterateL1ToL2Messages(range = {}) {
1393
+ return this.store.iterateL1ToL2Messages(range);
1394
+ }
1395
+ removeL1ToL2Messages(startIndex) {
1396
+ return this.store.removeL1ToL2Messages(startIndex);
1397
+ }
1398
+ getLastL1ToL2Message() {
1399
+ return this.store.getLastL1ToL2Message();
1400
+ }
1401
+ getPendingChainValidationStatus() {
1402
+ return this.store.getPendingChainValidationStatus();
1403
+ }
1404
+ setPendingChainValidationStatus(status) {
1405
+ this.#log.debug(`Setting pending chain validation status to valid ${status?.valid}`, status);
1406
+ return this.store.setPendingChainValidationStatus(status);
1407
+ }
900
1408
  }