@aztec/archiver 0.0.1-commit.b655e406 → 0.0.1-commit.d3ec352c
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.
- package/dest/archiver/archiver.d.ts +53 -40
- package/dest/archiver/archiver.d.ts.map +1 -1
- package/dest/archiver/archiver.js +333 -221
- package/dest/archiver/archiver_store.d.ts +16 -15
- package/dest/archiver/archiver_store.d.ts.map +1 -1
- package/dest/archiver/archiver_store_test_suite.d.ts +1 -1
- package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
- package/dest/archiver/archiver_store_test_suite.js +85 -84
- package/dest/archiver/config.d.ts +1 -1
- package/dest/archiver/config.d.ts.map +1 -1
- package/dest/archiver/config.js +5 -0
- package/dest/archiver/data_retrieval.d.ts +18 -17
- package/dest/archiver/data_retrieval.d.ts.map +1 -1
- package/dest/archiver/data_retrieval.js +111 -86
- package/dest/archiver/errors.d.ts +1 -1
- package/dest/archiver/errors.d.ts.map +1 -1
- package/dest/archiver/index.d.ts +1 -1
- package/dest/archiver/instrumentation.d.ts +3 -3
- package/dest/archiver/instrumentation.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/block_store.d.ts +9 -8
- package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/block_store.js +8 -7
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +1 -1
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +1 -1
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +18 -17
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/log_store.d.ts +1 -1
- package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/log_store.js +3 -2
- package/dest/archiver/kv_archiver_store/message_store.d.ts +1 -1
- package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
- package/dest/archiver/structs/data_retrieval.d.ts +1 -1
- package/dest/archiver/structs/inbox_message.d.ts +3 -3
- package/dest/archiver/structs/inbox_message.d.ts.map +1 -1
- package/dest/archiver/structs/inbox_message.js +2 -1
- package/dest/archiver/structs/published.d.ts +3 -2
- package/dest/archiver/structs/published.d.ts.map +1 -1
- package/dest/archiver/validation.d.ts +10 -4
- package/dest/archiver/validation.d.ts.map +1 -1
- package/dest/archiver/validation.js +29 -21
- package/dest/factory.d.ts +1 -1
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +3 -2
- package/dest/index.d.ts +2 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -1
- package/dest/rpc/index.d.ts +2 -2
- package/dest/test/index.d.ts +1 -1
- package/dest/test/mock_archiver.d.ts +14 -5
- package/dest/test/mock_archiver.d.ts.map +1 -1
- package/dest/test/mock_archiver.js +19 -10
- package/dest/test/mock_l1_to_l2_message_source.d.ts +4 -2
- package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
- package/dest/test/mock_l1_to_l2_message_source.js +7 -2
- package/dest/test/mock_l2_block_source.d.ts +14 -9
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +20 -7
- package/dest/test/mock_structs.d.ts +1 -1
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +3 -2
- package/package.json +17 -17
- package/src/archiver/archiver.ts +448 -290
- package/src/archiver/archiver_store.ts +19 -14
- package/src/archiver/archiver_store_test_suite.ts +111 -79
- package/src/archiver/config.ts +5 -0
- package/src/archiver/data_retrieval.ts +157 -125
- package/src/archiver/instrumentation.ts +2 -2
- package/src/archiver/kv_archiver_store/block_store.ts +17 -16
- package/src/archiver/kv_archiver_store/kv_archiver_store.ts +16 -15
- package/src/archiver/kv_archiver_store/log_store.ts +3 -2
- package/src/archiver/structs/inbox_message.ts +5 -4
- package/src/archiver/structs/published.ts +2 -1
- package/src/archiver/validation.ts +52 -27
- package/src/factory.ts +3 -2
- package/src/index.ts +1 -1
- package/src/test/mock_archiver.ts +22 -11
- package/src/test/mock_l1_to_l2_message_source.ts +9 -3
- package/src/test/mock_l2_block_source.ts +30 -13
- package/src/test/mock_structs.ts +3 -2
|
@@ -4,40 +4,42 @@ function _ts_decorate(decorators, target, key, desc) {
|
|
|
4
4
|
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
6
|
}
|
|
7
|
+
import { GENESIS_BLOCK_HEADER_HASH } from '@aztec/constants';
|
|
7
8
|
import { EpochCache } from '@aztec/epoch-cache';
|
|
8
9
|
import { BlockTagTooOldError, InboxContract, RollupContract, createEthereumChain } from '@aztec/ethereum';
|
|
9
10
|
import { maxBigint } from '@aztec/foundation/bigint';
|
|
11
|
+
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
10
12
|
import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
|
|
11
13
|
import { merge, pick } from '@aztec/foundation/collection';
|
|
12
14
|
import { Fr } from '@aztec/foundation/fields';
|
|
13
15
|
import { createLogger } from '@aztec/foundation/log';
|
|
14
16
|
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
15
17
|
import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
|
|
16
|
-
import { sleep } from '@aztec/foundation/sleep';
|
|
17
18
|
import { count } from '@aztec/foundation/string';
|
|
18
|
-
import { Timer, elapsed } from '@aztec/foundation/timer';
|
|
19
|
+
import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
|
|
19
20
|
import { ContractClassPublishedEvent, PrivateFunctionBroadcastedEvent, UtilityFunctionBroadcastedEvent } from '@aztec/protocol-contracts/class-registry';
|
|
20
21
|
import { ContractInstancePublishedEvent, ContractInstanceUpdatedEvent } from '@aztec/protocol-contracts/instance-registry';
|
|
21
|
-
import { L2BlockSourceEvents } from '@aztec/stdlib/block';
|
|
22
|
+
import { L2Block, L2BlockSourceEvents, PublishedL2Block } from '@aztec/stdlib/block';
|
|
22
23
|
import { computePublicBytecodeCommitment, isValidPrivateFunctionMembershipProof, isValidUtilityFunctionMembershipProof } from '@aztec/stdlib/contract';
|
|
23
24
|
import { getEpochAtSlot, getEpochNumberAtTimestamp, getSlotAtTimestamp, getSlotRangeForEpoch, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
24
|
-
import {
|
|
25
|
+
import { getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
|
|
25
26
|
import { EventEmitter } from 'events';
|
|
26
27
|
import groupBy from 'lodash.groupby';
|
|
27
28
|
import { createPublicClient, fallback, http } from 'viem';
|
|
28
|
-
import {
|
|
29
|
+
import { retrieveCheckpointsFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedToPublishedCheckpoint } from './data_retrieval.js';
|
|
29
30
|
import { InitialBlockNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
|
|
30
31
|
import { ArchiverInstrumentation } from './instrumentation.js';
|
|
31
|
-
import {
|
|
32
|
+
import { validateCheckpointAttestations } from './validation.js';
|
|
32
33
|
function mapArchiverConfig(config) {
|
|
33
34
|
return {
|
|
34
35
|
pollingIntervalMs: config.archiverPollingIntervalMS,
|
|
35
36
|
batchSize: config.archiverBatchSize,
|
|
36
|
-
skipValidateBlockAttestations: config.skipValidateBlockAttestations
|
|
37
|
+
skipValidateBlockAttestations: config.skipValidateBlockAttestations,
|
|
38
|
+
maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds
|
|
37
39
|
};
|
|
38
40
|
}
|
|
39
41
|
/**
|
|
40
|
-
* Pulls
|
|
42
|
+
* Pulls checkpoints in a non-blocking manner and provides interface for their retrieval.
|
|
41
43
|
* Responsible for handling robust L1 polling so that other components do not need to
|
|
42
44
|
* concern themselves with it.
|
|
43
45
|
*/ export class Archiver extends EventEmitter {
|
|
@@ -47,10 +49,11 @@ function mapArchiverConfig(config) {
|
|
|
47
49
|
config;
|
|
48
50
|
blobSinkClient;
|
|
49
51
|
epochCache;
|
|
52
|
+
dateProvider;
|
|
50
53
|
instrumentation;
|
|
51
54
|
l1constants;
|
|
52
55
|
log;
|
|
53
|
-
/** A loop in which we will be continually fetching new
|
|
56
|
+
/** A loop in which we will be continually fetching new checkpoints. */ runningPromise;
|
|
54
57
|
rollup;
|
|
55
58
|
inbox;
|
|
56
59
|
store;
|
|
@@ -68,13 +71,16 @@ function mapArchiverConfig(config) {
|
|
|
68
71
|
* @param pollingIntervalMs - The interval for polling for L1 logs (in milliseconds).
|
|
69
72
|
* @param store - An archiver data store for storage & retrieval of blocks, encrypted logs & contract data.
|
|
70
73
|
* @param log - A logger.
|
|
71
|
-
*/ constructor(publicClient, l1Addresses, dataStore, config, blobSinkClient, epochCache, instrumentation, l1constants, log = createLogger('archiver')){
|
|
72
|
-
super(), this.publicClient = publicClient, this.l1Addresses = l1Addresses, this.dataStore = dataStore, this.config = config, this.blobSinkClient = blobSinkClient, this.epochCache = epochCache, this.instrumentation = instrumentation, this.l1constants = l1constants, this.log = log, this.initialSyncComplete = false;
|
|
74
|
+
*/ constructor(publicClient, l1Addresses, dataStore, config, blobSinkClient, epochCache, dateProvider, instrumentation, l1constants, log = createLogger('archiver')){
|
|
75
|
+
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;
|
|
73
76
|
this.tracer = instrumentation.tracer;
|
|
74
77
|
this.store = new ArchiverStoreHelper(dataStore);
|
|
75
78
|
this.rollup = new RollupContract(publicClient, l1Addresses.rollupAddress);
|
|
76
79
|
this.inbox = new InboxContract(publicClient, l1Addresses.inboxAddress);
|
|
77
80
|
this.initialSyncPromise = promiseWithResolvers();
|
|
81
|
+
// Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
|
|
82
|
+
// are done as fast as possible. This then gets updated once the initial sync completes.
|
|
83
|
+
this.runningPromise = new RunningPromise(()=>this.sync(), this.log, this.config.pollingIntervalMs / 10, makeLoggingErrorHandler(this.log, NoBlobBodiesFoundError, BlockTagTooOldError));
|
|
78
84
|
}
|
|
79
85
|
/**
|
|
80
86
|
* Creates a new instance of the Archiver and blocks until it syncs from chain.
|
|
@@ -113,11 +119,12 @@ function mapArchiverConfig(config) {
|
|
|
113
119
|
};
|
|
114
120
|
const opts = merge({
|
|
115
121
|
pollingIntervalMs: 10_000,
|
|
116
|
-
batchSize: 100
|
|
122
|
+
batchSize: 100,
|
|
123
|
+
maxAllowedEthClientDriftSeconds: 300
|
|
117
124
|
}, mapArchiverConfig(config));
|
|
118
125
|
const epochCache = deps.epochCache ?? await EpochCache.create(config.l1Contracts.rollupAddress, config, deps);
|
|
119
126
|
const telemetry = deps.telemetry ?? getTelemetryClient();
|
|
120
|
-
const archiver = new Archiver(publicClient, config.l1Contracts, archiverStore, opts, deps.blobSinkClient, epochCache, await ArchiverInstrumentation.new(telemetry, ()=>archiverStore.estimateSize()), l1Constants);
|
|
127
|
+
const archiver = new Archiver(publicClient, config.l1Contracts, archiverStore, opts, deps.blobSinkClient, epochCache, deps.dateProvider ?? new DateProvider(), await ArchiverInstrumentation.new(telemetry, ()=>archiverStore.estimateSize()), l1Constants);
|
|
121
128
|
await archiver.start(blockUntilSynced);
|
|
122
129
|
return archiver;
|
|
123
130
|
}
|
|
@@ -128,48 +135,48 @@ function mapArchiverConfig(config) {
|
|
|
128
135
|
* Starts sync process.
|
|
129
136
|
* @param blockUntilSynced - If true, blocks until the archiver has fully synced.
|
|
130
137
|
*/ async start(blockUntilSynced) {
|
|
131
|
-
if (this.runningPromise) {
|
|
138
|
+
if (this.runningPromise.isRunning()) {
|
|
132
139
|
throw new Error('Archiver is already running');
|
|
133
140
|
}
|
|
134
141
|
await this.blobSinkClient.testSources();
|
|
142
|
+
await this.testEthereumNodeSynced();
|
|
143
|
+
// Log initial state for the archiver
|
|
144
|
+
const { l1StartBlock } = this.l1constants;
|
|
145
|
+
const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
|
|
146
|
+
const currentL2Block = await this.getBlockNumber();
|
|
147
|
+
this.log.info(`Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo} and L2 block ${currentL2Block}`, {
|
|
148
|
+
blocksSynchedTo,
|
|
149
|
+
messagesSynchedTo,
|
|
150
|
+
currentL2Block
|
|
151
|
+
});
|
|
152
|
+
// Start sync loop, and return the wait for initial sync if we are asked to block until synced
|
|
153
|
+
this.runningPromise.start();
|
|
135
154
|
if (blockUntilSynced) {
|
|
136
|
-
|
|
137
|
-
this.log.info(`Retrying initial archiver sync in ${this.config.pollingIntervalMs}ms`);
|
|
138
|
-
await sleep(this.config.pollingIntervalMs);
|
|
139
|
-
}
|
|
155
|
+
return this.waitForInitialSync();
|
|
140
156
|
}
|
|
141
|
-
this.runningPromise = new RunningPromise(()=>this.sync(false), this.log, this.config.pollingIntervalMs, makeLoggingErrorHandler(this.log, // Ignored errors will not log to the console
|
|
142
|
-
// We ignore NoBlobBodiesFound as the message may not have been passed to the blob sink yet
|
|
143
|
-
NoBlobBodiesFoundError));
|
|
144
|
-
this.runningPromise.start();
|
|
145
157
|
}
|
|
146
158
|
syncImmediate() {
|
|
147
|
-
if (!this.runningPromise) {
|
|
148
|
-
throw new Error('Archiver is not running');
|
|
149
|
-
}
|
|
150
159
|
return this.runningPromise.trigger();
|
|
151
160
|
}
|
|
152
161
|
waitForInitialSync() {
|
|
153
162
|
return this.initialSyncPromise.promise;
|
|
154
163
|
}
|
|
155
|
-
async
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
return
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
return false;
|
|
164
|
+
/** Checks that the ethereum node we are connected to has a latest timestamp no more than the allowed drift. Throw if not. */ async testEthereumNodeSynced() {
|
|
165
|
+
const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
|
|
166
|
+
if (maxAllowedDelay === 0) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const { number, timestamp: l1Timestamp } = await this.publicClient.getBlock({
|
|
170
|
+
includeTransactions: false
|
|
171
|
+
});
|
|
172
|
+
const currentTime = BigInt(this.dateProvider.nowInSeconds());
|
|
173
|
+
if (currentTime - l1Timestamp > BigInt(maxAllowedDelay)) {
|
|
174
|
+
throw new Error(`Ethereum node is out of sync (last block synced ${number} at ${l1Timestamp} vs current time ${currentTime})`);
|
|
168
175
|
}
|
|
169
176
|
}
|
|
170
177
|
/**
|
|
171
178
|
* Fetches logs from L1 contracts and processes them.
|
|
172
|
-
*/ async sync(
|
|
179
|
+
*/ async sync() {
|
|
173
180
|
/**
|
|
174
181
|
* We keep track of three "pointers" to L1 blocks:
|
|
175
182
|
* 1. the last L1 block that published an L2 block
|
|
@@ -179,8 +186,6 @@ function mapArchiverConfig(config) {
|
|
|
179
186
|
* We do this to deal with L1 data providers that are eventually consistent (e.g. Infura).
|
|
180
187
|
* We guard against seeing block X with no data at one point, and later, the provider processes the block and it has data.
|
|
181
188
|
* The archiver will stay back, until there's data on L1 that will move the pointers forward.
|
|
182
|
-
*
|
|
183
|
-
* This code does not handle reorgs.
|
|
184
189
|
*/ const { l1StartBlock, l1StartBlockHash } = this.l1constants;
|
|
185
190
|
const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = {
|
|
186
191
|
l1BlockNumber: l1StartBlock,
|
|
@@ -191,12 +196,12 @@ function mapArchiverConfig(config) {
|
|
|
191
196
|
});
|
|
192
197
|
const currentL1BlockNumber = currentL1Block.number;
|
|
193
198
|
const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
199
|
+
this.log.trace(`Starting new archiver sync iteration`, {
|
|
200
|
+
blocksSynchedTo,
|
|
201
|
+
messagesSynchedTo,
|
|
202
|
+
currentL1BlockNumber,
|
|
203
|
+
currentL1BlockHash
|
|
204
|
+
});
|
|
200
205
|
// ********** Ensuring Consistency of data pulled from L1 **********
|
|
201
206
|
/**
|
|
202
207
|
* There are a number of calls in this sync operation to L1 for retrieving
|
|
@@ -219,23 +224,40 @@ function mapArchiverConfig(config) {
|
|
|
219
224
|
const currentL1Timestamp = !this.l1Timestamp || !this.l1BlockNumber || this.l1BlockNumber !== currentL1BlockNumber ? (await this.publicClient.getBlock({
|
|
220
225
|
blockNumber: currentL1BlockNumber
|
|
221
226
|
})).timestamp : this.l1Timestamp;
|
|
222
|
-
//
|
|
227
|
+
// Warn if the latest L1 block timestamp is too old
|
|
228
|
+
const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
|
|
229
|
+
const now = this.dateProvider.nowInSeconds();
|
|
230
|
+
if (maxAllowedDelay > 0 && Number(currentL1Timestamp) <= now - maxAllowedDelay) {
|
|
231
|
+
this.log.warn(`Latest L1 block ${currentL1BlockNumber} timestamp ${currentL1Timestamp} is too old. Make sure your Ethereum node is synced.`, {
|
|
232
|
+
currentL1BlockNumber,
|
|
233
|
+
currentL1Timestamp,
|
|
234
|
+
now,
|
|
235
|
+
maxAllowedDelay
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
// ********** Events that are processed per checkpoint **********
|
|
223
239
|
if (currentL1BlockNumber > blocksSynchedTo) {
|
|
224
|
-
// First we retrieve new L2 blocks and store them in the DB. This will also update the
|
|
225
|
-
// pending chain validation status, proven
|
|
226
|
-
const rollupStatus = await this.
|
|
240
|
+
// First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
|
|
241
|
+
// pending chain validation status, proven checkpoint number, and synched L1 block number.
|
|
242
|
+
const rollupStatus = await this.handleCheckpoints(blocksSynchedTo, currentL1BlockNumber);
|
|
227
243
|
// Then we prune the current epoch if it'd reorg on next submission.
|
|
228
|
-
// Note that we don't do this before retrieving
|
|
229
|
-
//
|
|
244
|
+
// Note that we don't do this before retrieving checkpoints because we may need to retrieve
|
|
245
|
+
// checkpoints from more than 2 epochs ago, so we want to make sure we have the latest view of
|
|
230
246
|
// the chain locally before we start unwinding stuff. This can be optimized by figuring out
|
|
231
|
-
// up to which point we're pruning, and then requesting
|
|
232
|
-
const { rollupCanPrune } = await this.handleEpochPrune(rollupStatus.
|
|
233
|
-
//
|
|
247
|
+
// up to which point we're pruning, and then requesting checkpoints up to that point only.
|
|
248
|
+
const { rollupCanPrune } = await this.handleEpochPrune(rollupStatus.provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp);
|
|
249
|
+
// If the last checkpoint we processed had an invalid attestation, we manually advance the L1 syncpoint
|
|
250
|
+
// past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
|
|
251
|
+
// we get a valid checkpoint to advance the syncpoint.
|
|
252
|
+
if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
|
|
253
|
+
await this.store.setBlockSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
|
|
254
|
+
}
|
|
255
|
+
// And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
|
|
234
256
|
// We only do this if rollup cant prune on the next submission. Otherwise we will end up
|
|
235
|
-
// re-syncing the
|
|
257
|
+
// re-syncing the checkpoints we have just unwound above. We also dont do this if the last checkpoint is invalid,
|
|
236
258
|
// since the archiver will rightfully refuse to sync up to it.
|
|
237
259
|
if (!rollupCanPrune && rollupStatus.validationResult?.valid) {
|
|
238
|
-
await this.
|
|
260
|
+
await this.checkForNewCheckpointsBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
|
|
239
261
|
}
|
|
240
262
|
this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
|
|
241
263
|
}
|
|
@@ -244,14 +266,17 @@ function mapArchiverConfig(config) {
|
|
|
244
266
|
// but the corresponding blocks have not been processed (see #12631).
|
|
245
267
|
this.l1Timestamp = currentL1Timestamp;
|
|
246
268
|
this.l1BlockNumber = currentL1BlockNumber;
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
if (
|
|
250
|
-
this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete
|
|
269
|
+
// We resolve the initial sync only once we've caught up with the latest L1 block number (with 1 block grace)
|
|
270
|
+
// so if the initial sync took too long, we still go for another iteration.
|
|
271
|
+
if (!this.initialSyncComplete && currentL1BlockNumber + 1n >= await this.publicClient.getBlockNumber()) {
|
|
272
|
+
this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete`, {
|
|
251
273
|
l1BlockNumber: currentL1BlockNumber,
|
|
252
274
|
syncPoint: await this.store.getSynchPoint(),
|
|
253
275
|
...await this.getL2Tips()
|
|
254
276
|
});
|
|
277
|
+
this.runningPromise.setPollingIntervalMS(this.config.pollingIntervalMs);
|
|
278
|
+
this.initialSyncComplete = true;
|
|
279
|
+
this.initialSyncPromise.resolve();
|
|
255
280
|
}
|
|
256
281
|
}
|
|
257
282
|
/** Queries the rollup contract on whether a prune can be executed on the immediate next L1 block. */ async canPrune(currentL1BlockNumber, currentL1Timestamp) {
|
|
@@ -268,30 +293,30 @@ function mapArchiverConfig(config) {
|
|
|
268
293
|
}
|
|
269
294
|
return result;
|
|
270
295
|
}
|
|
271
|
-
/** Checks if there'd be a reorg for the next
|
|
296
|
+
/** Checks if there'd be a reorg for the next checkpoint submission and start pruning now. */ async handleEpochPrune(provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp) {
|
|
272
297
|
const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
|
|
273
|
-
const
|
|
274
|
-
const canPrune =
|
|
298
|
+
const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
|
|
299
|
+
const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
|
|
275
300
|
if (canPrune) {
|
|
276
301
|
const timer = new Timer();
|
|
277
|
-
const pruneFrom =
|
|
278
|
-
const header = await this.
|
|
302
|
+
const pruneFrom = CheckpointNumber(provenCheckpointNumber + 1);
|
|
303
|
+
const header = await this.getCheckpointHeader(pruneFrom);
|
|
279
304
|
if (header === undefined) {
|
|
280
|
-
throw new Error(`Missing
|
|
305
|
+
throw new Error(`Missing checkpoint header ${pruneFrom}`);
|
|
281
306
|
}
|
|
282
|
-
const pruneFromSlotNumber = header.
|
|
307
|
+
const pruneFromSlotNumber = header.slotNumber;
|
|
283
308
|
const pruneFromEpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
|
|
284
|
-
const
|
|
285
|
-
const
|
|
309
|
+
const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
|
|
310
|
+
const checkpoints = await this.getCheckpoints(pruneFrom, checkpointsToUnwind);
|
|
286
311
|
// Emit an event for listening services to react to the chain prune
|
|
287
312
|
this.emit(L2BlockSourceEvents.L2PruneDetected, {
|
|
288
313
|
type: L2BlockSourceEvents.L2PruneDetected,
|
|
289
314
|
epochNumber: pruneFromEpochNumber,
|
|
290
|
-
blocks
|
|
315
|
+
blocks: checkpoints.flatMap((c)=>L2Block.fromCheckpoint(c))
|
|
291
316
|
});
|
|
292
|
-
this.log.debug(`L2 prune from ${
|
|
293
|
-
await this.
|
|
294
|
-
this.log.warn(`Unwound ${count(
|
|
317
|
+
this.log.debug(`L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`);
|
|
318
|
+
await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
|
|
319
|
+
this.log.warn(`Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` + `to ${provenCheckpointNumber} due to predicted reorg at L1 block ${currentL1BlockNumber}. ` + `Updated latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`);
|
|
295
320
|
this.instrumentation.processPrune(timer.ms());
|
|
296
321
|
// TODO(palla/reorg): Do we need to set the block synched L1 block number here?
|
|
297
322
|
// Seems like the next iteration should handle this.
|
|
@@ -334,7 +359,7 @@ function mapArchiverConfig(config) {
|
|
|
334
359
|
});
|
|
335
360
|
// Compare message count and rolling hash. If they match, no need to retrieve anything.
|
|
336
361
|
if (remoteMessagesState.totalMessagesInserted === localMessagesInserted && remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO)) {
|
|
337
|
-
this.log.
|
|
362
|
+
this.log.trace(`No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`);
|
|
338
363
|
return;
|
|
339
364
|
}
|
|
340
365
|
// Check if our syncpoint is still valid. If not, there was an L1 reorg and we need to re-retrieve messages.
|
|
@@ -460,162 +485,173 @@ function mapArchiverConfig(config) {
|
|
|
460
485
|
}
|
|
461
486
|
return Buffer32.fromString(block.hash);
|
|
462
487
|
}
|
|
463
|
-
async
|
|
464
|
-
const
|
|
488
|
+
async handleCheckpoints(blocksSynchedTo, currentL1BlockNumber) {
|
|
489
|
+
const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
|
|
465
490
|
const initialValidationResult = await this.store.getPendingChainValidationStatus();
|
|
466
|
-
const [
|
|
491
|
+
const [rollupProvenCheckpointNumber, provenArchive, rollupPendingCheckpointNumber, pendingArchive, archiveForLocalPendingCheckpointNumber] = await this.rollup.status(localPendingCheckpointNumber, {
|
|
467
492
|
blockNumber: currentL1BlockNumber
|
|
468
493
|
});
|
|
494
|
+
const provenCheckpointNumber = CheckpointNumber.fromBigInt(rollupProvenCheckpointNumber);
|
|
495
|
+
const pendingCheckpointNumber = CheckpointNumber.fromBigInt(rollupPendingCheckpointNumber);
|
|
469
496
|
const rollupStatus = {
|
|
470
|
-
|
|
497
|
+
provenCheckpointNumber,
|
|
471
498
|
provenArchive,
|
|
472
|
-
|
|
499
|
+
pendingCheckpointNumber,
|
|
473
500
|
pendingArchive,
|
|
474
501
|
validationResult: initialValidationResult
|
|
475
502
|
};
|
|
476
503
|
this.log.trace(`Retrieved rollup status at current L1 block ${currentL1BlockNumber}.`, {
|
|
477
|
-
|
|
504
|
+
localPendingCheckpointNumber,
|
|
478
505
|
blocksSynchedTo,
|
|
479
506
|
currentL1BlockNumber,
|
|
480
|
-
|
|
507
|
+
archiveForLocalPendingCheckpointNumber,
|
|
481
508
|
...rollupStatus
|
|
482
509
|
});
|
|
483
|
-
const
|
|
484
|
-
// Annoying edge case: if proven
|
|
485
|
-
// we need to set it to zero. This is an edge case because we dont have a
|
|
486
|
-
// so
|
|
487
|
-
if (
|
|
488
|
-
const
|
|
489
|
-
if (
|
|
490
|
-
await this.
|
|
491
|
-
this.log.info(`Rolled back proven chain to
|
|
492
|
-
|
|
510
|
+
const updateProvenCheckpoint = async ()=>{
|
|
511
|
+
// Annoying edge case: if proven checkpoint is moved back to 0 due to a reorg at the beginning of the chain,
|
|
512
|
+
// we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
|
|
513
|
+
// so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
|
|
514
|
+
if (provenCheckpointNumber === 0) {
|
|
515
|
+
const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
|
|
516
|
+
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
517
|
+
await this.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
518
|
+
this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, {
|
|
519
|
+
provenCheckpointNumber
|
|
493
520
|
});
|
|
494
521
|
}
|
|
495
522
|
}
|
|
496
|
-
const
|
|
497
|
-
// Sanity check. I've hit what seems to be a state where the proven
|
|
498
|
-
// synched
|
|
499
|
-
const synched = await this.
|
|
500
|
-
if (
|
|
501
|
-
this.log.error(`Hit local
|
|
523
|
+
const localCheckpointForDestinationProvenCheckpointNumber = await this.getCheckpoint(provenCheckpointNumber);
|
|
524
|
+
// Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
|
|
525
|
+
// synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
|
|
526
|
+
const synched = await this.getSynchedCheckpointNumber();
|
|
527
|
+
if (localCheckpointForDestinationProvenCheckpointNumber && synched < localCheckpointForDestinationProvenCheckpointNumber.number) {
|
|
528
|
+
this.log.error(`Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.number} > ${synched}`);
|
|
502
529
|
}
|
|
503
|
-
this.log.trace(`Local
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
this.
|
|
509
|
-
|
|
530
|
+
this.log.trace(`Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'}`);
|
|
531
|
+
const lastProvenBlockNumber = await this.getLastBlockNumberInCheckpoint(provenCheckpointNumber);
|
|
532
|
+
if (localCheckpointForDestinationProvenCheckpointNumber && provenArchive === localCheckpointForDestinationProvenCheckpointNumber.archive.root.toString()) {
|
|
533
|
+
const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
|
|
534
|
+
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
535
|
+
await this.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
536
|
+
this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, {
|
|
537
|
+
provenCheckpointNumber
|
|
510
538
|
});
|
|
511
|
-
const provenSlotNumber =
|
|
539
|
+
const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
|
|
512
540
|
const provenEpochNumber = getEpochAtSlot(provenSlotNumber, this.l1constants);
|
|
513
541
|
this.emit(L2BlockSourceEvents.L2BlockProven, {
|
|
514
542
|
type: L2BlockSourceEvents.L2BlockProven,
|
|
515
|
-
blockNumber:
|
|
543
|
+
blockNumber: lastProvenBlockNumber,
|
|
516
544
|
slotNumber: provenSlotNumber,
|
|
517
545
|
epochNumber: provenEpochNumber
|
|
518
546
|
});
|
|
519
547
|
} else {
|
|
520
|
-
this.log.trace(`Proven
|
|
548
|
+
this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
|
|
521
549
|
}
|
|
522
550
|
}
|
|
523
|
-
this.instrumentation.updateLastProvenBlock(
|
|
551
|
+
this.instrumentation.updateLastProvenBlock(lastProvenBlockNumber);
|
|
524
552
|
};
|
|
525
|
-
// This is an edge case that we only hit if there are no proposed
|
|
526
|
-
// If we have 0
|
|
527
|
-
const
|
|
528
|
-
if (
|
|
553
|
+
// This is an edge case that we only hit if there are no proposed checkpoints.
|
|
554
|
+
// If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
|
|
555
|
+
const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
|
|
556
|
+
if (noCheckpoints) {
|
|
529
557
|
await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
|
|
530
|
-
this.log.debug(`No
|
|
558
|
+
this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`);
|
|
531
559
|
return rollupStatus;
|
|
532
560
|
}
|
|
533
|
-
await
|
|
561
|
+
await updateProvenCheckpoint();
|
|
534
562
|
// Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
|
|
535
|
-
// are any state that could be impacted by it. If we have no
|
|
536
|
-
if (
|
|
537
|
-
const
|
|
538
|
-
if (
|
|
539
|
-
throw new Error(`Missing
|
|
563
|
+
// are any state that could be impacted by it. If we have no checkpoints, there is no impact.
|
|
564
|
+
if (localPendingCheckpointNumber > 0) {
|
|
565
|
+
const localPendingCheckpoint = await this.getCheckpoint(localPendingCheckpointNumber);
|
|
566
|
+
if (localPendingCheckpoint === undefined) {
|
|
567
|
+
throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
|
|
540
568
|
}
|
|
541
|
-
const localPendingArchiveRoot =
|
|
542
|
-
const
|
|
543
|
-
if (
|
|
569
|
+
const localPendingArchiveRoot = localPendingCheckpoint.archive.root.toString();
|
|
570
|
+
const noCheckpointSinceLast = localPendingCheckpoint && pendingArchive === localPendingArchiveRoot;
|
|
571
|
+
if (noCheckpointSinceLast) {
|
|
544
572
|
// We believe the following line causes a problem when we encounter L1 re-orgs.
|
|
545
573
|
// Basically, by setting the synched L1 block number here, we are saying that we have
|
|
546
|
-
// processed all
|
|
574
|
+
// processed all checkpoints up to the current L1 block number and we will not attempt to retrieve logs from
|
|
547
575
|
// this block again (or any blocks before).
|
|
548
|
-
// However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing
|
|
576
|
+
// However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing checkpoints.
|
|
549
577
|
// We must only set this block number based on actually retrieved logs.
|
|
550
578
|
// TODO(#8621): Tackle this properly when we handle L1 Re-orgs.
|
|
551
579
|
// await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
|
|
552
|
-
this.log.debug(`No
|
|
580
|
+
this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
|
|
553
581
|
return rollupStatus;
|
|
554
582
|
}
|
|
555
|
-
const
|
|
556
|
-
if (!
|
|
557
|
-
// If our local pending
|
|
583
|
+
const localPendingCheckpointInChain = archiveForLocalPendingCheckpointNumber === localPendingArchiveRoot;
|
|
584
|
+
if (!localPendingCheckpointInChain) {
|
|
585
|
+
// If our local pending checkpoint tip is not in the chain on L1 a "prune" must have happened
|
|
558
586
|
// or the L1 have reorged.
|
|
559
587
|
// In any case, we have to figure out how far into the past the action will take us.
|
|
560
|
-
// For simplicity here, we will simply rewind until we end in a
|
|
561
|
-
this.log.debug(`L2 prune has been detected due to local pending
|
|
562
|
-
|
|
588
|
+
// For simplicity here, we will simply rewind until we end in a checkpoint that is also on the chain on L1.
|
|
589
|
+
this.log.debug(`L2 prune has been detected due to local pending checkpoint ${localPendingCheckpointNumber} not in chain`, {
|
|
590
|
+
localPendingCheckpointNumber,
|
|
563
591
|
localPendingArchiveRoot,
|
|
564
|
-
|
|
592
|
+
archiveForLocalPendingCheckpointNumber
|
|
565
593
|
});
|
|
566
|
-
let tipAfterUnwind =
|
|
594
|
+
let tipAfterUnwind = localPendingCheckpointNumber;
|
|
567
595
|
while(true){
|
|
568
|
-
const
|
|
569
|
-
if (
|
|
596
|
+
const candidateCheckpoint = await this.getCheckpoint(tipAfterUnwind);
|
|
597
|
+
if (candidateCheckpoint === undefined) {
|
|
570
598
|
break;
|
|
571
599
|
}
|
|
572
|
-
const archiveAtContract = await this.rollup.archiveAt(
|
|
573
|
-
|
|
600
|
+
const archiveAtContract = await this.rollup.archiveAt(candidateCheckpoint.number);
|
|
601
|
+
this.log.trace(`Checking local checkpoint ${candidateCheckpoint.number} with archive ${candidateCheckpoint.archive.root}`, {
|
|
602
|
+
archiveAtContract,
|
|
603
|
+
archiveLocal: candidateCheckpoint.archive.root.toString()
|
|
604
|
+
});
|
|
605
|
+
if (archiveAtContract === candidateCheckpoint.archive.root.toString()) {
|
|
574
606
|
break;
|
|
575
607
|
}
|
|
576
608
|
tipAfterUnwind--;
|
|
577
609
|
}
|
|
578
|
-
const
|
|
579
|
-
await this.
|
|
580
|
-
this.log.warn(`Unwound ${count(
|
|
610
|
+
const checkpointsToUnwind = localPendingCheckpointNumber - tipAfterUnwind;
|
|
611
|
+
await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
|
|
612
|
+
this.log.warn(`Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` + `due to mismatched checkpoint hashes at L1 block ${currentL1BlockNumber}. ` + `Updated L2 latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`);
|
|
581
613
|
}
|
|
582
614
|
}
|
|
583
|
-
// Retrieve
|
|
615
|
+
// Retrieve checkpoints in batches. Each batch is estimated to accommodate up to 'blockBatchSize' L1 blocks,
|
|
584
616
|
// computed using the L2 block time vs the L1 block time.
|
|
585
617
|
let searchStartBlock = blocksSynchedTo;
|
|
586
618
|
let searchEndBlock = blocksSynchedTo;
|
|
587
|
-
let
|
|
619
|
+
let lastRetrievedCheckpoint;
|
|
620
|
+
let lastL1BlockWithCheckpoint = undefined;
|
|
588
621
|
do {
|
|
589
622
|
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
|
|
590
|
-
this.log.trace(`Retrieving
|
|
623
|
+
this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
591
624
|
// TODO(md): Retrieve from blob sink then from consensus client, then from peers
|
|
592
|
-
const
|
|
593
|
-
if (
|
|
625
|
+
const retrievedCheckpoints = await retrieveCheckpointsFromRollup(this.rollup.getContract(), this.publicClient, this.blobSinkClient, searchStartBlock, searchEndBlock, this.log);
|
|
626
|
+
if (retrievedCheckpoints.length === 0) {
|
|
594
627
|
// We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
|
|
595
628
|
// See further details in earlier comments.
|
|
596
|
-
this.log.trace(`Retrieved no new
|
|
629
|
+
this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
597
630
|
continue;
|
|
598
631
|
}
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
632
|
+
this.log.debug(`Retrieved ${retrievedCheckpoints.length} new checkpoints between L1 blocks ${searchStartBlock} and ${searchEndBlock}`, {
|
|
633
|
+
lastProcessedCheckpoint: retrievedCheckpoints[retrievedCheckpoints.length - 1].l1,
|
|
634
|
+
searchStartBlock,
|
|
635
|
+
searchEndBlock
|
|
636
|
+
});
|
|
637
|
+
const publishedCheckpoints = await Promise.all(retrievedCheckpoints.map((b)=>retrievedToPublishedCheckpoint(b)));
|
|
638
|
+
const validCheckpoints = [];
|
|
639
|
+
for (const published of publishedCheckpoints){
|
|
604
640
|
const validationResult = this.config.skipValidateBlockAttestations ? {
|
|
605
641
|
valid: true
|
|
606
|
-
} : await
|
|
607
|
-
// Only update the validation result if it has changed, so we can keep track of the first invalid
|
|
608
|
-
// in case there is a sequence of more than one invalid
|
|
609
|
-
// There is an exception though: if
|
|
642
|
+
} : await validateCheckpointAttestations(published, this.epochCache, this.l1constants, this.log);
|
|
643
|
+
// Only update the validation result if it has changed, so we can keep track of the first invalid checkpoint
|
|
644
|
+
// in case there is a sequence of more than one invalid checkpoint, as we need to invalidate the first one.
|
|
645
|
+
// There is an exception though: if a checkpoint is invalidated and replaced with another invalid checkpoint,
|
|
610
646
|
// we need to update the validation result, since we need to be able to invalidate the new one.
|
|
611
|
-
// See test 'chain progresses if an invalid
|
|
647
|
+
// See test 'chain progresses if an invalid checkpoint is invalidated with an invalid one' for more info.
|
|
612
648
|
if (rollupStatus.validationResult?.valid !== validationResult.valid || !rollupStatus.validationResult.valid && !validationResult.valid && rollupStatus.validationResult.block.blockNumber === validationResult.block.blockNumber) {
|
|
613
649
|
rollupStatus.validationResult = validationResult;
|
|
614
650
|
}
|
|
615
651
|
if (!validationResult.valid) {
|
|
616
|
-
this.log.warn(`Skipping
|
|
617
|
-
|
|
618
|
-
l1BlockNumber:
|
|
652
|
+
this.log.warn(`Skipping checkpoint ${published.checkpoint.number} due to invalid attestations`, {
|
|
653
|
+
checkpointHash: published.checkpoint.hash(),
|
|
654
|
+
l1BlockNumber: published.l1.blockNumber,
|
|
619
655
|
...pick(validationResult, 'reason')
|
|
620
656
|
});
|
|
621
657
|
// Emit event for invalid block detection
|
|
@@ -625,22 +661,22 @@ function mapArchiverConfig(config) {
|
|
|
625
661
|
});
|
|
626
662
|
continue;
|
|
627
663
|
}
|
|
628
|
-
|
|
629
|
-
this.log.debug(`Ingesting new
|
|
630
|
-
|
|
631
|
-
l1BlockNumber:
|
|
632
|
-
...
|
|
633
|
-
|
|
664
|
+
validCheckpoints.push(published);
|
|
665
|
+
this.log.debug(`Ingesting new checkpoint ${published.checkpoint.number} with ${published.checkpoint.blocks.length} blocks`, {
|
|
666
|
+
checkpointHash: published.checkpoint.hash(),
|
|
667
|
+
l1BlockNumber: published.l1.blockNumber,
|
|
668
|
+
...published.checkpoint.header.toInspect(),
|
|
669
|
+
blocks: published.checkpoint.blocks.map((b)=>b.getStats())
|
|
634
670
|
});
|
|
635
671
|
}
|
|
636
672
|
try {
|
|
637
673
|
const updatedValidationResult = rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
|
|
638
|
-
const [processDuration] = await elapsed(()=>this.
|
|
639
|
-
this.instrumentation.processNewBlocks(processDuration /
|
|
674
|
+
const [processDuration] = await elapsed(()=>this.addCheckpoints(validCheckpoints, updatedValidationResult));
|
|
675
|
+
this.instrumentation.processNewBlocks(processDuration / validCheckpoints.length, validCheckpoints.flatMap((c)=>c.checkpoint.blocks));
|
|
640
676
|
} catch (err) {
|
|
641
677
|
if (err instanceof InitialBlockNumberNotSequentialError) {
|
|
642
678
|
const { previousBlockNumber, newBlockNumber } = err;
|
|
643
|
-
const previousBlock = previousBlockNumber ? await this.store.getPublishedBlock(previousBlockNumber) : undefined;
|
|
679
|
+
const previousBlock = previousBlockNumber ? await this.store.getPublishedBlock(BlockNumber(previousBlockNumber)) : undefined;
|
|
644
680
|
const updatedL1SyncPoint = previousBlock?.l1.blockNumber ?? this.l1constants.l1StartBlock;
|
|
645
681
|
await this.store.setBlockSynchedL1BlockNumber(updatedL1SyncPoint);
|
|
646
682
|
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.`, {
|
|
@@ -652,58 +688,58 @@ function mapArchiverConfig(config) {
|
|
|
652
688
|
}
|
|
653
689
|
throw err;
|
|
654
690
|
}
|
|
655
|
-
for (const
|
|
656
|
-
this.log.info(`Downloaded
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
691
|
+
for (const checkpoint of validCheckpoints){
|
|
692
|
+
this.log.info(`Downloaded checkpoint ${checkpoint.checkpoint.number}`, {
|
|
693
|
+
checkpointHash: checkpoint.checkpoint.hash(),
|
|
694
|
+
checkpointNumber: checkpoint.checkpoint.number,
|
|
695
|
+
blockCount: checkpoint.checkpoint.blocks.length,
|
|
696
|
+
txCount: checkpoint.checkpoint.blocks.reduce((acc, b)=>acc + b.body.txEffects.length, 0),
|
|
697
|
+
header: checkpoint.checkpoint.header.toInspect(),
|
|
698
|
+
archiveRoot: checkpoint.checkpoint.archive.root.toString(),
|
|
699
|
+
archiveNextLeafIndex: checkpoint.checkpoint.archive.nextAvailableLeafIndex
|
|
663
700
|
});
|
|
664
701
|
}
|
|
665
|
-
|
|
702
|
+
lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
|
|
703
|
+
lastL1BlockWithCheckpoint = publishedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
|
|
666
704
|
}while (searchEndBlock < currentL1BlockNumber)
|
|
667
705
|
// Important that we update AFTER inserting the blocks.
|
|
668
|
-
await
|
|
706
|
+
await updateProvenCheckpoint();
|
|
669
707
|
return {
|
|
670
708
|
...rollupStatus,
|
|
671
|
-
|
|
709
|
+
lastRetrievedCheckpoint,
|
|
710
|
+
lastL1BlockWithCheckpoint
|
|
672
711
|
};
|
|
673
712
|
}
|
|
674
|
-
async
|
|
675
|
-
const {
|
|
676
|
-
// Compare the last
|
|
713
|
+
async checkForNewCheckpointsBeforeL1SyncPoint(status, blocksSynchedTo, currentL1BlockNumber) {
|
|
714
|
+
const { lastRetrievedCheckpoint, pendingCheckpointNumber } = status;
|
|
715
|
+
// Compare the last checkpoint we have (either retrieved in this round or loaded from store) with what the
|
|
677
716
|
// rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
|
|
678
|
-
const
|
|
679
|
-
if (
|
|
717
|
+
const latestLocalCheckpointNumber = lastRetrievedCheckpoint?.checkpoint.number ?? await this.getSynchedCheckpointNumber();
|
|
718
|
+
if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
|
|
680
719
|
// Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
|
|
681
|
-
// but still
|
|
682
|
-
// We suspect an L1 reorg that added
|
|
683
|
-
// last
|
|
684
|
-
// don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
|
|
685
|
-
const
|
|
686
|
-
const targetL1BlockNumber =
|
|
687
|
-
const
|
|
688
|
-
this.log.warn(`Failed to reach
|
|
689
|
-
|
|
690
|
-
|
|
720
|
+
// but still haven't reached the pending checkpoint according to the call to the rollup contract.
|
|
721
|
+
// We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
|
|
722
|
+
// the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
|
|
723
|
+
// we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
|
|
724
|
+
const latestLocalCheckpoint = lastRetrievedCheckpoint ?? (latestLocalCheckpointNumber > 0 ? await this.getPublishedCheckpoints(latestLocalCheckpointNumber, 1).then(([c])=>c) : undefined);
|
|
725
|
+
const targetL1BlockNumber = latestLocalCheckpoint?.l1.blockNumber ?? maxBigint(currentL1BlockNumber - 64n, 0n);
|
|
726
|
+
const latestLocalCheckpointArchive = latestLocalCheckpoint?.checkpoint.archive.root.toString();
|
|
727
|
+
this.log.warn(`Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` + `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`, {
|
|
728
|
+
latestLocalCheckpointNumber,
|
|
729
|
+
latestLocalCheckpointArchive,
|
|
691
730
|
blocksSynchedTo,
|
|
692
731
|
currentL1BlockNumber,
|
|
693
732
|
...status
|
|
694
733
|
});
|
|
695
734
|
await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
|
|
696
735
|
} else {
|
|
697
|
-
this.log.trace(`No new
|
|
698
|
-
|
|
699
|
-
|
|
736
|
+
this.log.trace(`No new checkpoints behind L1 sync point to retrieve.`, {
|
|
737
|
+
latestLocalCheckpointNumber,
|
|
738
|
+
pendingCheckpointNumber
|
|
700
739
|
});
|
|
701
740
|
}
|
|
702
741
|
}
|
|
703
742
|
/** Resumes the archiver after a stop. */ resume() {
|
|
704
|
-
if (!this.runningPromise) {
|
|
705
|
-
throw new Error(`Archiver was never started`);
|
|
706
|
-
}
|
|
707
743
|
if (this.runningPromise.isRunning()) {
|
|
708
744
|
this.log.warn(`Archiver already running`);
|
|
709
745
|
}
|
|
@@ -715,7 +751,7 @@ function mapArchiverConfig(config) {
|
|
|
715
751
|
* @returns A promise signalling completion of the stop process.
|
|
716
752
|
*/ async stop() {
|
|
717
753
|
this.log.debug('Stopping...');
|
|
718
|
-
await this.runningPromise
|
|
754
|
+
await this.runningPromise.stop();
|
|
719
755
|
this.log.info('Stopped.');
|
|
720
756
|
return Promise.resolve();
|
|
721
757
|
}
|
|
@@ -754,12 +790,12 @@ function mapArchiverConfig(config) {
|
|
|
754
790
|
// Walk the list of blocks backwards and filter by slots matching the requested epoch.
|
|
755
791
|
// We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
|
|
756
792
|
let block = await this.getBlock(await this.store.getSynchedL2BlockNumber());
|
|
757
|
-
const slot = (b)=>b.header.globalVariables.slotNumber
|
|
793
|
+
const slot = (b)=>b.header.globalVariables.slotNumber;
|
|
758
794
|
while(block && slot(block) >= start){
|
|
759
795
|
if (slot(block) <= end) {
|
|
760
796
|
blocks.push(block);
|
|
761
797
|
}
|
|
762
|
-
block = await this.getBlock(block.number - 1);
|
|
798
|
+
block = await this.getBlock(BlockNumber(block.number - 1));
|
|
763
799
|
}
|
|
764
800
|
return blocks.reverse();
|
|
765
801
|
}
|
|
@@ -770,19 +806,20 @@ function mapArchiverConfig(config) {
|
|
|
770
806
|
// We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
|
|
771
807
|
let number = await this.store.getSynchedL2BlockNumber();
|
|
772
808
|
let header = await this.getBlockHeader(number);
|
|
773
|
-
const slot = (b)=>b.globalVariables.slotNumber
|
|
809
|
+
const slot = (b)=>b.globalVariables.slotNumber;
|
|
774
810
|
while(header && slot(header) >= start){
|
|
775
811
|
if (slot(header) <= end) {
|
|
776
812
|
blocks.push(header);
|
|
777
813
|
}
|
|
778
|
-
|
|
814
|
+
number = BlockNumber(number - 1);
|
|
815
|
+
header = await this.getBlockHeader(number);
|
|
779
816
|
}
|
|
780
817
|
return blocks.reverse();
|
|
781
818
|
}
|
|
782
819
|
async isEpochComplete(epochNumber) {
|
|
783
820
|
// The epoch is complete if the current L2 block is the last one in the epoch (or later)
|
|
784
821
|
const header = await this.getBlockHeader('latest');
|
|
785
|
-
const slot = header
|
|
822
|
+
const slot = header ? header.globalVariables.slotNumber : undefined;
|
|
786
823
|
const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1constants);
|
|
787
824
|
if (slot && slot >= endSlot) {
|
|
788
825
|
return true;
|
|
@@ -806,6 +843,83 @@ function mapArchiverConfig(config) {
|
|
|
806
843
|
/** Returns whether the archiver has completed an initial sync run successfully. */ isInitialSyncComplete() {
|
|
807
844
|
return this.initialSyncComplete;
|
|
808
845
|
}
|
|
846
|
+
async getPublishedCheckpoints(from, limit, proven) {
|
|
847
|
+
// TODO: Implement this properly. This only works when we have one block per checkpoint.
|
|
848
|
+
const blocks = await this.getPublishedBlocks(BlockNumber(from), limit, proven);
|
|
849
|
+
return blocks.map((b)=>b.toPublishedCheckpoint());
|
|
850
|
+
}
|
|
851
|
+
async getCheckpointByArchive(archive) {
|
|
852
|
+
// TODO: Implement this properly. This only works when we have one block per checkpoint.
|
|
853
|
+
return (await this.getPublishedBlockByArchive(archive))?.block.toCheckpoint();
|
|
854
|
+
}
|
|
855
|
+
async getCheckpoints(from, limit, proven) {
|
|
856
|
+
const published = await this.getPublishedCheckpoints(from, limit, proven);
|
|
857
|
+
return published.map((p)=>p.checkpoint);
|
|
858
|
+
}
|
|
859
|
+
async getCheckpoint(number) {
|
|
860
|
+
if (number < 0) {
|
|
861
|
+
number = await this.getSynchedCheckpointNumber();
|
|
862
|
+
}
|
|
863
|
+
if (number === 0) {
|
|
864
|
+
return undefined;
|
|
865
|
+
}
|
|
866
|
+
const published = await this.getPublishedCheckpoints(number, 1);
|
|
867
|
+
return published[0]?.checkpoint;
|
|
868
|
+
}
|
|
869
|
+
async getCheckpointHeader(number) {
|
|
870
|
+
if (number === 'latest') {
|
|
871
|
+
number = await this.getSynchedCheckpointNumber();
|
|
872
|
+
}
|
|
873
|
+
if (number === 0) {
|
|
874
|
+
return undefined;
|
|
875
|
+
}
|
|
876
|
+
const checkpoint = await this.getCheckpoint(number);
|
|
877
|
+
return checkpoint?.header;
|
|
878
|
+
}
|
|
879
|
+
getCheckpointNumber() {
|
|
880
|
+
return this.getSynchedCheckpointNumber();
|
|
881
|
+
}
|
|
882
|
+
async getSynchedCheckpointNumber() {
|
|
883
|
+
// TODO: Create store and apis for checkpoints.
|
|
884
|
+
// Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
|
|
885
|
+
return CheckpointNumber(await this.store.getSynchedL2BlockNumber());
|
|
886
|
+
}
|
|
887
|
+
async getProvenCheckpointNumber() {
|
|
888
|
+
// TODO: Create store and apis for checkpoints.
|
|
889
|
+
// Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
|
|
890
|
+
return CheckpointNumber(await this.store.getProvenL2BlockNumber());
|
|
891
|
+
}
|
|
892
|
+
setProvenCheckpointNumber(checkpointNumber) {
|
|
893
|
+
// TODO: Create store and apis for checkpoints.
|
|
894
|
+
// Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
|
|
895
|
+
return this.store.setProvenL2BlockNumber(BlockNumber.fromCheckpointNumber(checkpointNumber));
|
|
896
|
+
}
|
|
897
|
+
unwindCheckpoints(from, checkpointsToUnwind) {
|
|
898
|
+
// TODO: Create store and apis for checkpoints.
|
|
899
|
+
// This only works when we have one block per checkpoint.
|
|
900
|
+
return this.store.unwindBlocks(BlockNumber.fromCheckpointNumber(from), checkpointsToUnwind);
|
|
901
|
+
}
|
|
902
|
+
getLastBlockNumberInCheckpoint(checkpointNumber) {
|
|
903
|
+
// TODO: Create store and apis for checkpoints.
|
|
904
|
+
// Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
|
|
905
|
+
return Promise.resolve(BlockNumber.fromCheckpointNumber(checkpointNumber));
|
|
906
|
+
}
|
|
907
|
+
addCheckpoints(checkpoints, pendingChainValidationStatus) {
|
|
908
|
+
// TODO: Create store and apis for checkpoints.
|
|
909
|
+
// This only works when we have one block per checkpoint.
|
|
910
|
+
return this.store.addBlocks(checkpoints.map((p)=>PublishedL2Block.fromPublishedCheckpoint(p)), pendingChainValidationStatus);
|
|
911
|
+
}
|
|
912
|
+
async getCheckpointsForEpoch(epochNumber) {
|
|
913
|
+
// TODO: Create store and apis for checkpoints.
|
|
914
|
+
// This only works when we have one block per checkpoint.
|
|
915
|
+
const blocks = await this.getBlocksForEpoch(epochNumber);
|
|
916
|
+
return blocks.map((b)=>b.toCheckpoint());
|
|
917
|
+
}
|
|
918
|
+
getL1ToL2MessagesForCheckpoint(checkpointNumber) {
|
|
919
|
+
// TODO: Create dedicated api for checkpoints.
|
|
920
|
+
// This only works when we have one block per checkpoint.
|
|
921
|
+
return this.getL1ToL2Messages(BlockNumber.fromCheckpointNumber(checkpointNumber));
|
|
922
|
+
}
|
|
809
923
|
/**
|
|
810
924
|
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
811
925
|
* @param from - Number of the first block to return (inclusive).
|
|
@@ -960,7 +1074,7 @@ function mapArchiverConfig(config) {
|
|
|
960
1074
|
// TODO(#13569): Compute proper finalized block number based on L1 finalized block.
|
|
961
1075
|
// We just force it 2 epochs worth of proven data for now.
|
|
962
1076
|
// NOTE: update end-to-end/src/e2e_epochs/epochs_empty_blocks.test.ts as that uses finalized blocks in computations
|
|
963
|
-
const finalizedBlockNumber = Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0);
|
|
1077
|
+
const finalizedBlockNumber = BlockNumber(Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0));
|
|
964
1078
|
const [latestBlockHeader, provenBlockHeader, finalizedBlockHeader] = await Promise.all([
|
|
965
1079
|
latestBlockNumber > 0 ? this.getBlockHeader(latestBlockNumber) : undefined,
|
|
966
1080
|
provenBlockNumber > 0 ? this.getBlockHeader(provenBlockNumber) : undefined,
|
|
@@ -975,21 +1089,21 @@ function mapArchiverConfig(config) {
|
|
|
975
1089
|
if (finalizedBlockNumber > 0 && !finalizedBlockHeader) {
|
|
976
1090
|
throw new Error(`Failed to retrieve finalized block header for block ${finalizedBlockNumber} (latest block is ${latestBlockNumber})`);
|
|
977
1091
|
}
|
|
978
|
-
const latestBlockHeaderHash = await latestBlockHeader?.hash();
|
|
979
|
-
const provenBlockHeaderHash = await provenBlockHeader?.hash();
|
|
980
|
-
const finalizedBlockHeaderHash = await finalizedBlockHeader?.hash();
|
|
1092
|
+
const latestBlockHeaderHash = await latestBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
|
|
1093
|
+
const provenBlockHeaderHash = await provenBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
|
|
1094
|
+
const finalizedBlockHeaderHash = await finalizedBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
|
|
981
1095
|
return {
|
|
982
1096
|
latest: {
|
|
983
1097
|
number: latestBlockNumber,
|
|
984
|
-
hash: latestBlockHeaderHash
|
|
1098
|
+
hash: latestBlockHeaderHash.toString()
|
|
985
1099
|
},
|
|
986
1100
|
proven: {
|
|
987
1101
|
number: provenBlockNumber,
|
|
988
|
-
hash: provenBlockHeaderHash
|
|
1102
|
+
hash: provenBlockHeaderHash.toString()
|
|
989
1103
|
},
|
|
990
1104
|
finalized: {
|
|
991
1105
|
number: finalizedBlockNumber,
|
|
992
|
-
hash: finalizedBlockHeaderHash
|
|
1106
|
+
hash: finalizedBlockHeaderHash.toString()
|
|
993
1107
|
}
|
|
994
1108
|
};
|
|
995
1109
|
}
|
|
@@ -1009,7 +1123,7 @@ function mapArchiverConfig(config) {
|
|
|
1009
1123
|
const targetL1BlockNumber = targetL2Block.l1.blockNumber;
|
|
1010
1124
|
const targetL1BlockHash = await this.getL1BlockHash(targetL1BlockNumber);
|
|
1011
1125
|
this.log.info(`Unwinding ${blocksToUnwind} blocks from L2 block ${currentL2Block}`);
|
|
1012
|
-
await this.store.unwindBlocks(currentL2Block, blocksToUnwind);
|
|
1126
|
+
await this.store.unwindBlocks(BlockNumber(currentL2Block), blocksToUnwind);
|
|
1013
1127
|
this.log.info(`Unwinding L1 to L2 messages to ${targetL2BlockNumber}`);
|
|
1014
1128
|
await this.store.rollbackL1ToL2MessagesToL2Block(targetL2BlockNumber);
|
|
1015
1129
|
this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
|
|
@@ -1020,7 +1134,7 @@ function mapArchiverConfig(config) {
|
|
|
1020
1134
|
});
|
|
1021
1135
|
if (targetL2BlockNumber < currentProvenBlock) {
|
|
1022
1136
|
this.log.info(`Clearing proven L2 block number`);
|
|
1023
|
-
await this.store.setProvenL2BlockNumber(
|
|
1137
|
+
await this.store.setProvenL2BlockNumber(BlockNumber.ZERO);
|
|
1024
1138
|
}
|
|
1025
1139
|
// TODO(palla/reorg): Set the finalized block when we add support for it.
|
|
1026
1140
|
// if (targetL2BlockNumber < currentFinalizedBlock) {
|
|
@@ -1030,9 +1144,7 @@ function mapArchiverConfig(config) {
|
|
|
1030
1144
|
}
|
|
1031
1145
|
}
|
|
1032
1146
|
_ts_decorate([
|
|
1033
|
-
trackSpan('Archiver.sync'
|
|
1034
|
-
[Attributes.INITIAL_SYNC]: initialRun
|
|
1035
|
-
}))
|
|
1147
|
+
trackSpan('Archiver.sync')
|
|
1036
1148
|
], Archiver.prototype, "sync", null);
|
|
1037
1149
|
var Operation = /*#__PURE__*/ function(Operation) {
|
|
1038
1150
|
Operation[Operation["Store"] = 0] = "Store";
|
|
@@ -1187,7 +1299,7 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
1187
1299
|
throw new Error(`Cannot unwind ${blocksToUnwind} blocks`);
|
|
1188
1300
|
}
|
|
1189
1301
|
// from - blocksToUnwind = the new head, so + 1 for what we need to remove
|
|
1190
|
-
const blocks = await this.getPublishedBlocks(from - blocksToUnwind + 1, blocksToUnwind);
|
|
1302
|
+
const blocks = await this.getPublishedBlocks(BlockNumber(from - blocksToUnwind + 1), blocksToUnwind);
|
|
1191
1303
|
const opResults = await Promise.all([
|
|
1192
1304
|
// Prune rolls back to the last proven block, which is by definition valid
|
|
1193
1305
|
this.store.setPendingChainValidationStatus({
|