@aztec-labs/archiver 6.0.0-rc.3 → 6.0.0-rc.4
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.d.ts +11 -2
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +94 -2
- package/dest/config.js +1 -1
- package/dest/errors.d.ts +12 -1
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +12 -0
- package/dest/l1/bin/retrieve-calldata.js +7 -5
- package/dest/l1/calldata_retriever.d.ts +5 -2
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +8 -3
- package/dest/l1/data_retrieval.d.ts +2 -1
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +14 -9
- package/dest/l1/validate_trace.d.ts +3 -1
- package/dest/l1/validate_trace.d.ts.map +1 -1
- package/dest/l1/validate_trace.js +8 -3
- package/dest/modules/checkpoint_ingestor.d.ts +71 -0
- package/dest/modules/checkpoint_ingestor.d.ts.map +1 -0
- package/dest/modules/checkpoint_ingestor.js +516 -0
- package/dest/modules/data_store_updater.d.ts +6 -11
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +4 -3
- package/dest/modules/instrumentation.d.ts +8 -4
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +8 -2
- package/dest/modules/l1_synchronizer.d.ts +3 -7
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +84 -420
- package/dest/modules/pending_chain_validation_tracker.d.ts +21 -0
- package/dest/modules/pending_chain_validation_tracker.d.ts.map +1 -0
- package/dest/modules/pending_chain_validation_tracker.js +32 -0
- package/dest/modules/validation.d.ts +2 -2
- package/dest/modules/validation.d.ts.map +1 -1
- package/dest/modules/validation.js +17 -11
- package/dest/store/block_store.d.ts +1 -1
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +8 -2
- package/dest/store/log_store_codec.d.ts +2 -1
- package/dest/store/log_store_codec.d.ts.map +1 -1
- package/dest/store/log_store_codec.js +1 -0
- package/dest/test/fake_l1_state.d.ts +21 -1
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +82 -30
- package/package.json +15 -15
- package/src/archiver.ts +112 -3
- package/src/config.ts +1 -1
- package/src/errors.ts +21 -0
- package/src/l1/README.md +5 -0
- package/src/l1/bin/retrieve-calldata.ts +5 -3
- package/src/l1/calldata_retriever.ts +8 -3
- package/src/l1/data_retrieval.ts +13 -11
- package/src/l1/validate_trace.ts +11 -2
- package/src/modules/checkpoint_ingestor.ts +742 -0
- package/src/modules/data_store_updater.ts +7 -13
- package/src/modules/instrumentation.ts +17 -3
- package/src/modules/l1_synchronizer.ts +116 -563
- package/src/modules/pending_chain_validation_tracker.ts +43 -0
- package/src/modules/validation.ts +13 -9
- package/src/store/block_store.ts +6 -0
- package/src/store/log_store_codec.ts +1 -0
- package/src/test/fake_l1_state.ts +94 -31
|
@@ -0,0 +1,742 @@
|
|
|
1
|
+
import type { BlobClientInterface } from '@aztec-labs/blob-client/client';
|
|
2
|
+
import { type EpochCache, requireStableCommittee } from '@aztec-labs/epoch-cache';
|
|
3
|
+
import type { RollupContract } from '@aztec-labs/ethereum/contracts';
|
|
4
|
+
import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec-labs/ethereum/types';
|
|
5
|
+
import { asyncPool } from '@aztec-labs/foundation/async-pool';
|
|
6
|
+
import { BlockNumber, CheckpointNumber, type SlotNumber } from '@aztec-labs/foundation/branded-types';
|
|
7
|
+
import { partition, pick } from '@aztec-labs/foundation/collection';
|
|
8
|
+
import { EthAddress } from '@aztec-labs/foundation/eth-address';
|
|
9
|
+
import type { Logger } from '@aztec-labs/foundation/log';
|
|
10
|
+
import { elapsed } from '@aztec-labs/foundation/timer';
|
|
11
|
+
import {
|
|
12
|
+
type ArchiverEmitter,
|
|
13
|
+
type L2Block,
|
|
14
|
+
L2BlockSourceEvents,
|
|
15
|
+
type ValidateCheckpointResult,
|
|
16
|
+
} from '@aztec-labs/stdlib/block';
|
|
17
|
+
import {
|
|
18
|
+
Checkpoint,
|
|
19
|
+
type CheckpointInfo,
|
|
20
|
+
type ProposedCheckpointData,
|
|
21
|
+
PublishedCheckpoint,
|
|
22
|
+
} from '@aztec-labs/stdlib/checkpoint';
|
|
23
|
+
import { type L1RollupConstants, getEpochAtSlot, getTimestampForSlot } from '@aztec-labs/stdlib/epoch-helpers';
|
|
24
|
+
import type { CoordinationSignatureContext } from '@aztec-labs/stdlib/p2p';
|
|
25
|
+
import { type Tracer, execInSpan } from '@aztec-labs/telemetry-client';
|
|
26
|
+
|
|
27
|
+
import { InitialCheckpointNumberNotSequentialError } from '../errors.js';
|
|
28
|
+
import {
|
|
29
|
+
type ResolvedCheckpointFromCalldata,
|
|
30
|
+
type RetrievedCheckpointFromCalldata,
|
|
31
|
+
getCheckpointBlobDataFromBlobs,
|
|
32
|
+
retrieveCheckpointCalldataFromRollup,
|
|
33
|
+
retrievedToPublishedCheckpoint,
|
|
34
|
+
} from '../l1/data_retrieval.js';
|
|
35
|
+
import type { RejectedCheckpoint, RejectedCheckpointReason } from '../store/block_store.js';
|
|
36
|
+
import type { ArchiverDataStores } from '../store/data_stores.js';
|
|
37
|
+
import type { ArchiverDataStoreUpdater } from './data_store_updater.js';
|
|
38
|
+
import type { ArchiverInstrumentation } from './instrumentation.js';
|
|
39
|
+
import { PendingChainValidationTracker } from './pending_chain_validation_tracker.js';
|
|
40
|
+
import { resolveCheckpointAttestationsFromCalldata } from './validation.js';
|
|
41
|
+
|
|
42
|
+
/** A checkpoint observed in L1 calldata this iteration, whether ingested or rejected. */
|
|
43
|
+
export type SeenCheckpoint = Pick<RejectedCheckpoint, 'checkpointNumber' | 'l1'>;
|
|
44
|
+
|
|
45
|
+
/** How the checkpoint L1 published relates to the locally proposed checkpoint of the same number, if any. */
|
|
46
|
+
type ProposedCheckpointMatch =
|
|
47
|
+
| { kind: 'promote'; checkpoint: PublishedCheckpoint }
|
|
48
|
+
| { kind: 'evict'; fromCheckpointNumber: CheckpointNumber }
|
|
49
|
+
| { kind: 'none' };
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Everything one batch decided before it is persisted: what to add, what to promote or evict, and the status to
|
|
53
|
+
* persist. Screening has already recorded the batch's rejected checkpoints by then.
|
|
54
|
+
*/
|
|
55
|
+
type BatchPlan = {
|
|
56
|
+
/** Accepted checkpoints in L1 order, blob-fetched or promoted. Drives metrics and logs. */
|
|
57
|
+
published: PublishedCheckpoint[];
|
|
58
|
+
/** The accepted checkpoints to persist from their L1 payloads (`published` minus the promoted one). */
|
|
59
|
+
toAdd: PublishedCheckpoint[];
|
|
60
|
+
/** The promoted checkpoint, when the match was a promotion and the checkpoint passed screening. */
|
|
61
|
+
promoted: PublishedCheckpoint | undefined;
|
|
62
|
+
/** First proposed checkpoint to evict, when the local proposed copy diverged from L1's. */
|
|
63
|
+
evictProposedFrom: CheckpointNumber | undefined;
|
|
64
|
+
/** The pending chain validation status to persist, if it moved this iteration. */
|
|
65
|
+
validationUpdate: ValidateCheckpointResult | undefined;
|
|
66
|
+
/** The batch's last checkpoint on L1, whether accepted or rejected. */
|
|
67
|
+
lastSeenCheckpoint: SeenCheckpoint;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/** Collaborators the checkpoint ingestor shares with the L1 synchronizer. */
|
|
71
|
+
export type CheckpointIngestorDeps = {
|
|
72
|
+
rollup: RollupContract;
|
|
73
|
+
publicClient: ViemPublicClient;
|
|
74
|
+
debugClient: ViemPublicDebugClient;
|
|
75
|
+
blobClient: BlobClientInterface;
|
|
76
|
+
epochCache: EpochCache;
|
|
77
|
+
stores: ArchiverDataStores;
|
|
78
|
+
updater: ArchiverDataStoreUpdater;
|
|
79
|
+
events: ArchiverEmitter;
|
|
80
|
+
instrumentation: ArchiverInstrumentation;
|
|
81
|
+
l1Constants: L1RollupConstants;
|
|
82
|
+
tracer: Tracer;
|
|
83
|
+
log: Logger;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** Ingestion settings, read on every pass so config updates apply to the next one. */
|
|
87
|
+
export type CheckpointIngestorOptions = {
|
|
88
|
+
batchSizeInL1Blocks: bigint;
|
|
89
|
+
skipValidateCheckpointAttestations: boolean;
|
|
90
|
+
skipPromoteProposedCheckpointDuringL1Sync: boolean;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Ingests the checkpoints L1 published in a range of L1 blocks: retrieves their calldata in batches, screens
|
|
95
|
+
* attestations and rejected ancestry from calldata alone, promotes a matching locally proposed checkpoint instead of
|
|
96
|
+
* fetching its blobs, fetches blobs for the rest and persists each batch. It rewinds the L1 sync point when a batch
|
|
97
|
+
* turns out not to be consecutive with the stored chain, and never leaves it past where a batch started if the batch
|
|
98
|
+
* fails before it is persisted.
|
|
99
|
+
*/
|
|
100
|
+
export class CheckpointIngestor {
|
|
101
|
+
constructor(
|
|
102
|
+
private readonly deps: CheckpointIngestorDeps,
|
|
103
|
+
private readonly getOptions: () => CheckpointIngestorOptions,
|
|
104
|
+
) {}
|
|
105
|
+
|
|
106
|
+
/** Ingests every checkpoint published in the L1 block range (from, to], batch by batch, in L1 order. */
|
|
107
|
+
public async ingest(
|
|
108
|
+
from: bigint,
|
|
109
|
+
to: bigint,
|
|
110
|
+
opts: { initialSyncComplete: boolean; initialValidationStatus: ValidateCheckpointResult | undefined },
|
|
111
|
+
): Promise<{ blocksAdded: L2Block[]; lastSeenCheckpoint: SeenCheckpoint | undefined }> {
|
|
112
|
+
const validation = new PendingChainValidationTracker(opts.initialValidationStatus);
|
|
113
|
+
const blocksAdded: L2Block[] = [];
|
|
114
|
+
let lastSeenCheckpoint: SeenCheckpoint | undefined;
|
|
115
|
+
|
|
116
|
+
// Retrieve checkpoints in batches. Each batch is estimated to accommodate up to 'blockBatchSize' L1 blocks,
|
|
117
|
+
// computed using the L2 block time vs the L1 block time.
|
|
118
|
+
let searchStartBlock: bigint = from;
|
|
119
|
+
let searchEndBlock: bigint = from;
|
|
120
|
+
do {
|
|
121
|
+
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, to);
|
|
122
|
+
const calldataCheckpoints = await this.retrieveCalldata(searchStartBlock, searchEndBlock);
|
|
123
|
+
if (calldataCheckpoints.length === 0) {
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
const { plan, added } = await this.ingestBatch(
|
|
127
|
+
calldataCheckpoints,
|
|
128
|
+
{ searchStartBlock, searchEndBlock },
|
|
129
|
+
validation,
|
|
130
|
+
opts.initialSyncComplete,
|
|
131
|
+
);
|
|
132
|
+
blocksAdded.push(...added);
|
|
133
|
+
lastSeenCheckpoint = plan.lastSeenCheckpoint;
|
|
134
|
+
} while (searchEndBlock < to);
|
|
135
|
+
|
|
136
|
+
return { blocksAdded, lastSeenCheckpoint };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private getSignatureContext(): CoordinationSignatureContext {
|
|
140
|
+
return {
|
|
141
|
+
chainId: this.deps.publicClient.chain.id,
|
|
142
|
+
rollupAddress: EthAddress.fromString(this.deps.rollup.address),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private nextRange(end: bigint, limit: bigint): [bigint, bigint] {
|
|
147
|
+
const nextStart = end + 1n;
|
|
148
|
+
const nextEnd = nextStart + this.getOptions().batchSizeInL1Blocks;
|
|
149
|
+
if (nextEnd > limit) {
|
|
150
|
+
return [nextStart, limit];
|
|
151
|
+
}
|
|
152
|
+
return [nextStart, nextEnd];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Retrieves the calldata of the checkpoints published in an L1 block range, sorted by L1 block. Blobs are not
|
|
157
|
+
* fetched yet, since we may be able to just get that data out of the proposed chain.
|
|
158
|
+
*/
|
|
159
|
+
private async retrieveCalldata(
|
|
160
|
+
searchStartBlock: bigint,
|
|
161
|
+
searchEndBlock: bigint,
|
|
162
|
+
): Promise<RetrievedCheckpointFromCalldata[]> {
|
|
163
|
+
this.deps.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
164
|
+
|
|
165
|
+
const calldataCheckpoints = await execInSpan(
|
|
166
|
+
this.deps.tracer,
|
|
167
|
+
'Archiver.retrieveCheckpointCalldataFromRollup',
|
|
168
|
+
() =>
|
|
169
|
+
retrieveCheckpointCalldataFromRollup(
|
|
170
|
+
this.deps.rollup,
|
|
171
|
+
this.deps.publicClient,
|
|
172
|
+
this.deps.debugClient,
|
|
173
|
+
searchStartBlock, // TODO(palla/reorg): If the L2 reorg was due to an L1 reorg, we need to start search earlier
|
|
174
|
+
searchEndBlock,
|
|
175
|
+
this.deps.instrumentation,
|
|
176
|
+
this.deps.log,
|
|
177
|
+
),
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
if (calldataCheckpoints.length === 0) {
|
|
181
|
+
// We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
|
|
182
|
+
// See further details in the TODO(#8621) comment in ArchiverL1Synchronizer.reconcileCheckpointedChain.
|
|
183
|
+
this.deps.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
184
|
+
return [];
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
this.deps.log.debug(
|
|
188
|
+
`Retrieved ${calldataCheckpoints.length} new checkpoint calldata between L1 blocks ${searchStartBlock} and ${searchEndBlock}`,
|
|
189
|
+
{
|
|
190
|
+
lastProcessedCheckpoint: calldataCheckpoints[calldataCheckpoints.length - 1].l1,
|
|
191
|
+
searchStartBlock,
|
|
192
|
+
searchEndBlock,
|
|
193
|
+
},
|
|
194
|
+
);
|
|
195
|
+
return calldataCheckpoints;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Plans and persists one non-empty batch of retrieved checkpoints.
|
|
200
|
+
*
|
|
201
|
+
* Screening records rejected checkpoints as it goes, and each such write advances the L1 sync point past the
|
|
202
|
+
* rejected checkpoint's L1 block before the batch's valid checkpoints are persisted. If anything in the batch
|
|
203
|
+
* throws before the batch is persisted, the sync point is restored to where it stood before the batch, so the next
|
|
204
|
+
* iteration processes the whole batch again instead of skipping its valid checkpoints. Once the batch is persisted
|
|
205
|
+
* the sync point is left alone: replaying it would refetch blobs for checkpoints already stored, including promoted
|
|
206
|
+
* ones this node never fetched blobs for, and stall if those blobs are unavailable.
|
|
207
|
+
*/
|
|
208
|
+
private async ingestBatch(
|
|
209
|
+
calldataCheckpoints: RetrievedCheckpointFromCalldata[],
|
|
210
|
+
range: { searchStartBlock: bigint; searchEndBlock: bigint },
|
|
211
|
+
validation: PendingChainValidationTracker,
|
|
212
|
+
initialSyncComplete: boolean,
|
|
213
|
+
): Promise<{ plan: BatchPlan; added: L2Block[] }> {
|
|
214
|
+
const syncPointBeforeBatch = await this.deps.stores.blocks.getSynchedL1BlockNumber();
|
|
215
|
+
const progress = { persisted: false };
|
|
216
|
+
try {
|
|
217
|
+
const plan = await this.planBatch(calldataCheckpoints, validation, initialSyncComplete);
|
|
218
|
+
const added = await this.persistBatch(plan, progress);
|
|
219
|
+
this.logDownloaded(plan.published);
|
|
220
|
+
return { plan, added };
|
|
221
|
+
} catch (err) {
|
|
222
|
+
await this.restoreSyncPointAfterFailedBatch(syncPointBeforeBatch, range, progress);
|
|
223
|
+
throw err;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Decides everything about a batch before touching the store, other than recording rejected checkpoints. The
|
|
229
|
+
* proposed-copy lookup runs on the last checkpoint before screening, so a diverging local copy is evicted (and its
|
|
230
|
+
* equivocation reported) even when L1's checkpoint is then rejected, and a matching copy whose L1 checkpoint is
|
|
231
|
+
* rejected is not promoted.
|
|
232
|
+
*/
|
|
233
|
+
private async planBatch(
|
|
234
|
+
calldataCheckpoints: RetrievedCheckpointFromCalldata[],
|
|
235
|
+
validation: PendingChainValidationTracker,
|
|
236
|
+
initialSyncComplete: boolean,
|
|
237
|
+
): Promise<BatchPlan> {
|
|
238
|
+
// Decode and validate attestations from CALLDATA before fetching any blobs, so a checkpoint with invalid
|
|
239
|
+
// attestations is rejected without decoding a possibly malformed blob. Runs even when validation is
|
|
240
|
+
// disabled, since promotion and persistence need the decoded attestations.
|
|
241
|
+
const resolvedCheckpoints = await asyncPool(10, calldataCheckpoints, async calldataCheckpoint => {
|
|
242
|
+
const { attestations, validationResult } = await resolveCheckpointAttestationsFromCalldata(
|
|
243
|
+
calldataCheckpoint,
|
|
244
|
+
this.deps.epochCache,
|
|
245
|
+
this.deps.l1Constants,
|
|
246
|
+
this.getSignatureContext(),
|
|
247
|
+
this.deps.log,
|
|
248
|
+
);
|
|
249
|
+
return { checkpoint: { ...calldataCheckpoint, attestations }, validationResult };
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
// Check if the last checkpoint matches a local pending entry (so we can skip blob fetch).
|
|
253
|
+
// We only check the last one; if it matches, the blob fetch is skipped for that entry.
|
|
254
|
+
// TODO(palla/pipelining): We may have more than a single checkpoint to promote
|
|
255
|
+
const lastCalldataCheckpoint = resolvedCheckpoints[resolvedCheckpoints.length - 1].checkpoint;
|
|
256
|
+
const match = await this.matchProposedCheckpoint(lastCalldataCheckpoint);
|
|
257
|
+
const checkpointToPromote = match.kind === 'promote' ? match.checkpoint : undefined;
|
|
258
|
+
|
|
259
|
+
const checkpointsToIngest = await this.screen(resolvedCheckpoints, validation);
|
|
260
|
+
const published = await this.buildPublished(checkpointsToIngest, checkpointToPromote, initialSyncComplete);
|
|
261
|
+
|
|
262
|
+
// Split valid checkpoints: the promoted one (if any) is persisted via the proposed-promotion path,
|
|
263
|
+
// the rest via addCheckpoints. Both paths run within the same store transaction for atomicity.
|
|
264
|
+
const [[promoted], toAdd] = partition(
|
|
265
|
+
published,
|
|
266
|
+
c => c.checkpoint.number === checkpointToPromote?.checkpoint.number,
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
return {
|
|
270
|
+
published,
|
|
271
|
+
toAdd,
|
|
272
|
+
promoted,
|
|
273
|
+
evictProposedFrom: match.kind === 'evict' ? match.fromCheckpointNumber : undefined,
|
|
274
|
+
validationUpdate: validation.update,
|
|
275
|
+
// The last checkpoint seen on L1 this batch (valid or rejected), tracked from calldata since
|
|
276
|
+
// rejected checkpoints are no longer built into PublishedCheckpoints.
|
|
277
|
+
lastSeenCheckpoint: pick(lastCalldataCheckpoint, 'checkpointNumber', 'l1'),
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Screens checkpoints by their calldata attestation verdicts before fetching any blobs. A checkpoint with invalid
|
|
283
|
+
* attestations (or one descending from a rejected ancestor) is rejected here without fetching its blobs, so a
|
|
284
|
+
* malformed blob does not throw during decode before the rejection path runs and stall sync.
|
|
285
|
+
*
|
|
286
|
+
* Rejected checkpoints are recorded as they are found, outside the transaction that persists the batch, so a later
|
|
287
|
+
* checkpoint in the same batch can find its rejected ancestor. Each record advances the L1 sync point past it.
|
|
288
|
+
* Returns the accepted checkpoints in L1 order.
|
|
289
|
+
*/
|
|
290
|
+
private async screen(
|
|
291
|
+
resolvedCheckpoints: { checkpoint: ResolvedCheckpointFromCalldata; validationResult: ValidateCheckpointResult }[],
|
|
292
|
+
validation: PendingChainValidationTracker,
|
|
293
|
+
): Promise<ResolvedCheckpointFromCalldata[]> {
|
|
294
|
+
const checkpointsToIngest: ResolvedCheckpointFromCalldata[] = [];
|
|
295
|
+
// Archive roots accepted earlier in this batch. The store only drops a rejected entry for a root when the batch
|
|
296
|
+
// is persisted, so an accepted checkpoint whose root was also rejected (an earlier post of the same checkpoint
|
|
297
|
+
// with invalid attestations) would otherwise still look rejected to the children that follow it in this batch.
|
|
298
|
+
const acceptedArchiveRoots = new Set<string>();
|
|
299
|
+
|
|
300
|
+
for (const { checkpoint: calldataCheckpoint, validationResult: resolvedValidationResult } of resolvedCheckpoints) {
|
|
301
|
+
// Check the attestations uploaded by the publisher to L1 are correct.
|
|
302
|
+
// Rollup contract does not validate attestations to save on gas, so this
|
|
303
|
+
// falls on the nodes to verify offchain and skip those checkpoints.
|
|
304
|
+
const validationResult = this.getOptions().skipValidateCheckpointAttestations
|
|
305
|
+
? { valid: true as const }
|
|
306
|
+
: resolvedValidationResult;
|
|
307
|
+
|
|
308
|
+
// Also skip the checkpoint if it builds on a previously-rejected ancestor. Without
|
|
309
|
+
// this, addCheckpoints would throw InitialCheckpointNumberNotSequentialError when the
|
|
310
|
+
// ancestor was skipped earlier (e.g. due to invalid attestations), the catch handler
|
|
311
|
+
// would roll back the L1 sync point, and the next iteration would re-fetch and re-throw.
|
|
312
|
+
const parentArchiveRoot = calldataCheckpoint.header.lastArchiveRoot;
|
|
313
|
+
const rejectedAncestor = acceptedArchiveRoots.has(parentArchiveRoot.toString())
|
|
314
|
+
? undefined
|
|
315
|
+
: await this.deps.stores.blocks.getRejectedCheckpointByArchiveRoot(parentArchiveRoot);
|
|
316
|
+
|
|
317
|
+
validation.observe(validationResult, { hasRejectedAncestor: rejectedAncestor !== undefined });
|
|
318
|
+
|
|
319
|
+
if (!validationResult.valid) {
|
|
320
|
+
this.deps.log.warn(`Skipping checkpoint ${calldataCheckpoint.checkpointNumber} due to invalid attestations`, {
|
|
321
|
+
checkpointNumber: calldataCheckpoint.checkpointNumber,
|
|
322
|
+
l1BlockNumber: calldataCheckpoint.l1.blockNumber,
|
|
323
|
+
...pick(validationResult, 'reason'),
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
// Emit event for invalid checkpoint detection
|
|
327
|
+
this.deps.events.emit(L2BlockSourceEvents.InvalidAttestationsCheckpointDetected, {
|
|
328
|
+
type: L2BlockSourceEvents.InvalidAttestationsCheckpointDetected,
|
|
329
|
+
validationResult,
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
// Persist a rejected-ancestor entry so any later checkpoint that builds on this one
|
|
333
|
+
// is detected and skipped (rather than tripping the addCheckpoints consecutive-number
|
|
334
|
+
// check and causing the sync point to roll back in a loop).
|
|
335
|
+
await this.recordRejected(calldataCheckpoint, 'invalid-attestations');
|
|
336
|
+
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (rejectedAncestor) {
|
|
341
|
+
const descendantInfo: CheckpointInfo = {
|
|
342
|
+
archive: calldataCheckpoint.archiveRoot,
|
|
343
|
+
lastArchive: calldataCheckpoint.header.lastArchiveRoot,
|
|
344
|
+
slotNumber: calldataCheckpoint.header.slotNumber,
|
|
345
|
+
checkpointNumber: calldataCheckpoint.checkpointNumber,
|
|
346
|
+
timestamp: calldataCheckpoint.header.timestamp,
|
|
347
|
+
};
|
|
348
|
+
this.deps.log.warn(
|
|
349
|
+
`Skipping checkpoint ${calldataCheckpoint.checkpointNumber} as it is a descendant of ` +
|
|
350
|
+
`rejected checkpoint ${rejectedAncestor.checkpointNumber} (${rejectedAncestor.reason})`,
|
|
351
|
+
{
|
|
352
|
+
checkpointNumber: calldataCheckpoint.checkpointNumber,
|
|
353
|
+
l1BlockNumber: calldataCheckpoint.l1.blockNumber,
|
|
354
|
+
l1BlockHash: calldataCheckpoint.l1.blockHash,
|
|
355
|
+
ancestorCheckpointNumber: rejectedAncestor.checkpointNumber,
|
|
356
|
+
ancestorArchiveRoot: rejectedAncestor.archiveRoot.toString(),
|
|
357
|
+
ancestorReason: rejectedAncestor.reason,
|
|
358
|
+
},
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
this.deps.events.emit(L2BlockSourceEvents.DescendentOfInvalidAttestationsCheckpointDetected, {
|
|
362
|
+
type: L2BlockSourceEvents.DescendentOfInvalidAttestationsCheckpointDetected,
|
|
363
|
+
checkpoint: descendantInfo,
|
|
364
|
+
ancestorArchiveRoot: rejectedAncestor.archiveRoot,
|
|
365
|
+
ancestorCheckpointNumber: rejectedAncestor.checkpointNumber,
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
// Persist this chainpoint as rejected as well, so we can construct a chain of
|
|
369
|
+
// skipped checkpoints starting from the first one with invalid attestations.
|
|
370
|
+
await this.recordRejected(calldataCheckpoint, 'descends-from-invalid-attestations');
|
|
371
|
+
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
checkpointsToIngest.push(calldataCheckpoint);
|
|
376
|
+
acceptedArchiveRoots.add(calldataCheckpoint.archiveRoot.toString());
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return checkpointsToIngest;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
private recordRejected(calldataCheckpoint: RetrievedCheckpointFromCalldata, reason: RejectedCheckpointReason) {
|
|
383
|
+
return this.deps.stores.blocks.addRejectedCheckpoint({
|
|
384
|
+
checkpointNumber: calldataCheckpoint.checkpointNumber,
|
|
385
|
+
archiveRoot: calldataCheckpoint.archiveRoot,
|
|
386
|
+
parentArchiveRoot: calldataCheckpoint.header.lastArchiveRoot,
|
|
387
|
+
slotNumber: calldataCheckpoint.header.slotNumber,
|
|
388
|
+
l1: calldataCheckpoint.l1,
|
|
389
|
+
reason,
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** Builds the published checkpoints for the accepted ones, in L1 order, fetching blobs for all but the promoted one. */
|
|
394
|
+
private async buildPublished(
|
|
395
|
+
checkpointsToIngest: ResolvedCheckpointFromCalldata[],
|
|
396
|
+
checkpointToPromote: PublishedCheckpoint | undefined,
|
|
397
|
+
initialSyncComplete: boolean,
|
|
398
|
+
): Promise<PublishedCheckpoint[]> {
|
|
399
|
+
// Fetch blobs in parallel only for the surviving (attestation-valid, non-descendant) checkpoints,
|
|
400
|
+
// then build the full published checkpoints. The last calldata checkpoint may be promotable from a
|
|
401
|
+
// local proposed block (checkpointToPromote), in which case it carries no blob to fetch. A missing or
|
|
402
|
+
// undecodable blob throws and propagates, and the batch guard restores the L1 sync point so the fetch is retried.
|
|
403
|
+
const toFetchBlobs = checkpointToPromote
|
|
404
|
+
? checkpointsToIngest.filter(c => c.checkpointNumber !== checkpointToPromote.checkpoint.number)
|
|
405
|
+
: checkpointsToIngest;
|
|
406
|
+
const blobFetched = await asyncPool(10, toFetchBlobs, async checkpoint =>
|
|
407
|
+
retrievedToPublishedCheckpoint({
|
|
408
|
+
...checkpoint,
|
|
409
|
+
checkpointBlobData: await getCheckpointBlobDataFromBlobs(
|
|
410
|
+
this.deps.blobClient,
|
|
411
|
+
checkpoint.l1.blockHash,
|
|
412
|
+
checkpoint.blobHashes,
|
|
413
|
+
checkpoint.checkpointNumber,
|
|
414
|
+
this.deps.log,
|
|
415
|
+
!initialSyncComplete,
|
|
416
|
+
checkpoint.parentBeaconBlockRoot,
|
|
417
|
+
checkpoint.l1.timestamp,
|
|
418
|
+
),
|
|
419
|
+
}),
|
|
420
|
+
);
|
|
421
|
+
|
|
422
|
+
// Index the built checkpoints by number so we can ingest them in calldata order, slotting in the
|
|
423
|
+
// promoted checkpoint (built from a local proposed block rather than blobs).
|
|
424
|
+
const publishedByNumber = new Map(
|
|
425
|
+
blobFetched.map(publishedCheckpoint => [publishedCheckpoint.checkpoint.number, publishedCheckpoint]),
|
|
426
|
+
);
|
|
427
|
+
if (checkpointToPromote) {
|
|
428
|
+
publishedByNumber.set(checkpointToPromote.checkpoint.number, checkpointToPromote);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const validCheckpoints: PublishedCheckpoint[] = [];
|
|
432
|
+
for (const calldataCheckpoint of checkpointsToIngest) {
|
|
433
|
+
const published = publishedByNumber.get(calldataCheckpoint.checkpointNumber)!;
|
|
434
|
+
|
|
435
|
+
validCheckpoints.push(published);
|
|
436
|
+
this.deps.log.debug(
|
|
437
|
+
`Ingesting new checkpoint ${published.checkpoint.number} with ${published.checkpoint.blocks.length} blocks`,
|
|
438
|
+
{
|
|
439
|
+
checkpointHash: published.checkpoint.hash(),
|
|
440
|
+
l1BlockNumber: published.l1.blockNumber,
|
|
441
|
+
...published.checkpoint.header.toInspect(),
|
|
442
|
+
blocks: published.checkpoint.blocks.map(b => b.getStats()),
|
|
443
|
+
},
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
for (const published of validCheckpoints) {
|
|
448
|
+
this.deps.instrumentation.processCheckpointL1Timing({
|
|
449
|
+
slotNumber: published.checkpoint.header.slotNumber,
|
|
450
|
+
l1Timestamp: published.l1.timestamp,
|
|
451
|
+
l1Constants: this.deps.l1Constants,
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
return validCheckpoints;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Persists a planned batch in one store transaction, then reports its blocks and any local blocks it pruned.
|
|
460
|
+
* Marks `progress.persisted` once `addCheckpoints` has returned, which is after its transaction committed and the
|
|
461
|
+
* frontier cache refreshed, so a failed refresh still counts as not persisted. Rewinds the L1 sync point if the batch
|
|
462
|
+
* is not consecutive with the stored chain. Returns the blocks persisted from L1 payloads.
|
|
463
|
+
*/
|
|
464
|
+
private async persistBatch(plan: BatchPlan, progress: { persisted: boolean }): Promise<L2Block[]> {
|
|
465
|
+
try {
|
|
466
|
+
const [processDuration, result] = await elapsed(() =>
|
|
467
|
+
execInSpan(this.deps.tracer, 'Archiver.addCheckpoints', async () => {
|
|
468
|
+
const addResult = await this.deps.updater.addCheckpoints(
|
|
469
|
+
plan.toAdd,
|
|
470
|
+
plan.validationUpdate,
|
|
471
|
+
plan.promoted,
|
|
472
|
+
plan.evictProposedFrom,
|
|
473
|
+
);
|
|
474
|
+
// Set before the span wrappers finish, so a throw while closing the span doesn't count as unpersisted.
|
|
475
|
+
progress.persisted = true;
|
|
476
|
+
return addResult;
|
|
477
|
+
}),
|
|
478
|
+
);
|
|
479
|
+
|
|
480
|
+
if (plan.published.length > 0) {
|
|
481
|
+
this.deps.instrumentation.processNewCheckpointedBlocks(
|
|
482
|
+
processDuration / plan.published.length,
|
|
483
|
+
plan.published.flatMap(c => c.checkpoint.blocks),
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// Record blocks newly fetched from L1 checkpoint payloads as added. The promoted checkpoint (if any) is
|
|
488
|
+
// excluded: its blocks were already in local archiver storage (added via the proposed-block path) so the
|
|
489
|
+
// block stream does not need them re-downloaded.
|
|
490
|
+
const added = plan.toAdd.flatMap(c => c.checkpoint.blocks);
|
|
491
|
+
|
|
492
|
+
// If blocks were pruned due to conflict with L1 checkpoints, emit event
|
|
493
|
+
if (result.prunedBlocks && result.prunedBlocks.length > 0) {
|
|
494
|
+
const prunedCheckpointNumber = result.prunedBlocks[0].checkpointNumber;
|
|
495
|
+
const prunedSlotNumber = result.prunedBlocks[0].header.globalVariables.slotNumber;
|
|
496
|
+
|
|
497
|
+
this.deps.log.info(
|
|
498
|
+
`Pruned ${result.prunedBlocks.length} mismatching blocks for checkpoint ${prunedCheckpointNumber}`,
|
|
499
|
+
{ prunedBlocks: result.prunedBlocks.map(b => b.toBlockInfo()), prunedSlotNumber, prunedCheckpointNumber },
|
|
500
|
+
);
|
|
501
|
+
|
|
502
|
+
this.deps.instrumentation.recordPrune('l1_conflict');
|
|
503
|
+
|
|
504
|
+
// Emit event for listening services to react to the prune.
|
|
505
|
+
// Note: slotNumber comes from the first pruned block. If pruned blocks theoretically spanned multiple slots,
|
|
506
|
+
// only one slot number would be reported (though in practice all blocks in a checkpoint span a single slot).
|
|
507
|
+
this.deps.events.emit(L2BlockSourceEvents.L2PruneUncheckpointed, {
|
|
508
|
+
type: L2BlockSourceEvents.L2PruneUncheckpointed,
|
|
509
|
+
slotNumber: prunedSlotNumber,
|
|
510
|
+
blocks: result.prunedBlocks,
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
return added;
|
|
515
|
+
} catch (err) {
|
|
516
|
+
if (err instanceof InitialCheckpointNumberNotSequentialError) {
|
|
517
|
+
await this.rewindSyncPointAfterGap(err);
|
|
518
|
+
}
|
|
519
|
+
throw err;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Moves the L1 sync point back to the previous checkpoint's L1 block (falling back to the finalized checkpoint's,
|
|
525
|
+
* then to the rollup's start block), so the next iteration fetches the checkpoints missing before the batch.
|
|
526
|
+
*/
|
|
527
|
+
private async rewindSyncPointAfterGap(err: InitialCheckpointNumberNotSequentialError): Promise<void> {
|
|
528
|
+
const { previousCheckpointNumber, newCheckpointNumber } = err;
|
|
529
|
+
const previousCheckpoint = previousCheckpointNumber
|
|
530
|
+
? await this.deps.stores.blocks.getCheckpointData(CheckpointNumber(previousCheckpointNumber))
|
|
531
|
+
: undefined;
|
|
532
|
+
const lastFinalizedCheckpoint = await this.deps.stores.blocks.getCheckpointData(
|
|
533
|
+
await this.deps.stores.blocks.getFinalizedCheckpointNumber(),
|
|
534
|
+
);
|
|
535
|
+
const updatedL1SyncPoint =
|
|
536
|
+
previousCheckpoint?.l1.blockNumber ??
|
|
537
|
+
lastFinalizedCheckpoint?.l1.blockNumber ??
|
|
538
|
+
this.deps.l1Constants.l1StartBlock;
|
|
539
|
+
await this.deps.stores.blocks.setSynchedL1BlockNumber(updatedL1SyncPoint);
|
|
540
|
+
this.deps.log.warn(
|
|
541
|
+
`Attempting to insert checkpoint ${newCheckpointNumber} with previous block ${previousCheckpointNumber}. Rolling back L1 sync point to ${updatedL1SyncPoint} to try and fetch the missing blocks.`,
|
|
542
|
+
{
|
|
543
|
+
previousCheckpointNumber,
|
|
544
|
+
previousCheckpoint: previousCheckpoint?.header.toInspect(),
|
|
545
|
+
lastFinalizedCheckpoint: lastFinalizedCheckpoint?.header.toInspect(),
|
|
546
|
+
l1StartBlock: this.deps.l1Constants.l1StartBlock,
|
|
547
|
+
newCheckpointNumber,
|
|
548
|
+
updatedL1SyncPoint,
|
|
549
|
+
},
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/** Lowers the L1 sync point back to where it stood before a failed batch, unless the batch was persisted. */
|
|
554
|
+
private async restoreSyncPointAfterFailedBatch(
|
|
555
|
+
syncPointBeforeBatch: bigint | undefined,
|
|
556
|
+
{ searchStartBlock, searchEndBlock }: { searchStartBlock: bigint; searchEndBlock: bigint },
|
|
557
|
+
progress: { persisted: boolean },
|
|
558
|
+
): Promise<void> {
|
|
559
|
+
const restoreTo = syncPointBeforeBatch ?? this.deps.l1Constants.l1StartBlock;
|
|
560
|
+
const currentSyncPoint = await this.deps.stores.blocks.getSynchedL1BlockNumber();
|
|
561
|
+
// Only ever lower it, so a deeper rewind made while handling the error (e.g. on a checkpoint gap) is kept.
|
|
562
|
+
if (!progress.persisted && currentSyncPoint !== undefined && currentSyncPoint > restoreTo) {
|
|
563
|
+
this.deps.log.warn(`Restoring L1 sync point to ${restoreTo} after failing to process checkpoints`, {
|
|
564
|
+
currentSyncPoint,
|
|
565
|
+
restoreTo,
|
|
566
|
+
searchStartBlock,
|
|
567
|
+
searchEndBlock,
|
|
568
|
+
});
|
|
569
|
+
await this.deps.stores.blocks.setSynchedL1BlockNumber(restoreTo);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
private logDownloaded(published: PublishedCheckpoint[]): void {
|
|
574
|
+
for (const checkpoint of published) {
|
|
575
|
+
this.deps.log.info(`Downloaded checkpoint ${checkpoint.checkpoint.number}`, {
|
|
576
|
+
checkpointHash: checkpoint.checkpoint.hash(),
|
|
577
|
+
checkpointNumber: checkpoint.checkpoint.number,
|
|
578
|
+
blockCount: checkpoint.checkpoint.blocks.length,
|
|
579
|
+
txCount: checkpoint.checkpoint.blocks.reduce((acc, b) => acc + b.body.txEffects.length, 0),
|
|
580
|
+
header: checkpoint.checkpoint.header.toInspect(),
|
|
581
|
+
archiveRoot: checkpoint.checkpoint.archive.root.toString(),
|
|
582
|
+
archiveNextLeafIndex: checkpoint.checkpoint.archive.nextAvailableLeafIndex,
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Reports equivocation for a local proposed checkpoint that diverges from the L1 checkpoint at the same slot, but
|
|
589
|
+
* only when one key signed both. Two checkpoints for one slot from different proposers are not equivocation: a
|
|
590
|
+
* proposal made before the slot's proposer was final can be superseded by the real proposer's checkpoint on L1.
|
|
591
|
+
* Never throws, so a failed lookup leaves the divergence unattributed without holding up sync.
|
|
592
|
+
*/
|
|
593
|
+
private async reportEquivocationIfSameSigner(
|
|
594
|
+
proposed: ProposedCheckpointData,
|
|
595
|
+
calldataCheckpoint: ResolvedCheckpointFromCalldata,
|
|
596
|
+
): Promise<void> {
|
|
597
|
+
const { checkpointNumber } = calldataCheckpoint;
|
|
598
|
+
const { slotNumber } = calldataCheckpoint.header;
|
|
599
|
+
|
|
600
|
+
let l1Proposer: EthAddress | undefined;
|
|
601
|
+
if (proposed.signer) {
|
|
602
|
+
try {
|
|
603
|
+
l1Proposer = await this.getL1ProposerForSlot(slotNumber);
|
|
604
|
+
} catch (err) {
|
|
605
|
+
this.deps.log.warn(`Failed to get the L1 proposer for slot ${slotNumber}`, {
|
|
606
|
+
checkpointNumber,
|
|
607
|
+
slotNumber,
|
|
608
|
+
err,
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
if (!proposed.signer || !l1Proposer || !proposed.signer.equals(l1Proposer)) {
|
|
614
|
+
this.deps.log.warn(
|
|
615
|
+
`Not attributing equivocation for checkpoint ${checkpointNumber}: the local proposed copy and the L1 checkpoint have no common signer`,
|
|
616
|
+
{
|
|
617
|
+
checkpointNumber,
|
|
618
|
+
slotNumber,
|
|
619
|
+
proposedSigner: proposed.signer?.toString(),
|
|
620
|
+
l1Proposer: l1Proposer?.toString(),
|
|
621
|
+
},
|
|
622
|
+
);
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
this.deps.events.emit(L2BlockSourceEvents.CheckpointEquivocationDetected, {
|
|
627
|
+
type: L2BlockSourceEvents.CheckpointEquivocationDetected,
|
|
628
|
+
slotNumber,
|
|
629
|
+
checkpointNumber,
|
|
630
|
+
l1ArchiveRoot: calldataCheckpoint.archiveRoot,
|
|
631
|
+
proposedArchiveRoot: proposed.archive.root,
|
|
632
|
+
signer: proposed.signer,
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* Asks the rollup who had to sign the checkpoint it accepted for a past slot. `propose` only succeeds when the slot's
|
|
638
|
+
* proposer signed the checkpoint, and the rollup derives that proposer from history keyed by timestamp, so querying
|
|
639
|
+
* at the latest L1 block returns the address `propose` checked. The propose calldata cannot tell this: the
|
|
640
|
+
* CheckpointProposed event commits to neither the signers nor the bundle signature, so a reverted copy of the call
|
|
641
|
+
* signed by another key matches the event just as well. Returns undefined when L1 did not check the committee's
|
|
642
|
+
* proposer, because an escape hatch was open for the epoch, or when the committee was empty. Throws while the epoch's
|
|
643
|
+
* committee is not stable, since whether its escape hatch was open is not known yet.
|
|
644
|
+
*/
|
|
645
|
+
private async getL1ProposerForSlot(slot: SlotNumber): Promise<EthAddress | undefined> {
|
|
646
|
+
const epoch = getEpochAtSlot(slot, this.deps.l1Constants);
|
|
647
|
+
const { isEscapeHatchOpen } = requireStableCommittee(await this.deps.epochCache.getCommitteeForEpoch(epoch));
|
|
648
|
+
if (isEscapeHatchOpen) {
|
|
649
|
+
return undefined;
|
|
650
|
+
}
|
|
651
|
+
const proposer = await this.deps.rollup.getProposerAt(getTimestampForSlot(slot, this.deps.l1Constants));
|
|
652
|
+
return proposer.isZero() ? undefined : proposer;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* Checks if a specific checkpoint matches a local pending entry, and if so, loads local data to build
|
|
657
|
+
* a synthetic published checkpoint (skipping blob fetch), returned as a `promote` match.
|
|
658
|
+
*
|
|
659
|
+
* Returns an `evict` match when the L1 checkpoint does NOT match local pending data for that number, so the
|
|
660
|
+
* caller can evict the entire pending suffix >= fromCheckpointNumber (those entries chain off the now-invalid
|
|
661
|
+
* local state) within the same addCheckpoints transaction.
|
|
662
|
+
*/
|
|
663
|
+
private async matchProposedCheckpoint(
|
|
664
|
+
calldataCheckpoint: ResolvedCheckpointFromCalldata | undefined,
|
|
665
|
+
): Promise<ProposedCheckpointMatch> {
|
|
666
|
+
if (this.getOptions().skipPromoteProposedCheckpointDuringL1Sync || !calldataCheckpoint) {
|
|
667
|
+
return { kind: 'none' };
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// Look up the specific pending entry for the checkpoint being mined, not just the tip
|
|
671
|
+
const proposed = await this.deps.stores.blocks.getProposedCheckpointByNumber(calldataCheckpoint.checkpointNumber);
|
|
672
|
+
if (!proposed) {
|
|
673
|
+
return { kind: 'none' };
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
if (
|
|
677
|
+
!proposed.header.equals(calldataCheckpoint.header) ||
|
|
678
|
+
!proposed.archive.root.equals(calldataCheckpoint.archiveRoot) ||
|
|
679
|
+
proposed.feeAssetPriceModifier !== calldataCheckpoint.feeAssetPriceModifier
|
|
680
|
+
) {
|
|
681
|
+
this.deps.log.warn(
|
|
682
|
+
`Local proposed checkpoint ${proposed.checkpointNumber} does not match checkpoint retrieved from L1, overriding with L1 data`,
|
|
683
|
+
{
|
|
684
|
+
proposedCheckpointNumber: proposed.checkpointNumber,
|
|
685
|
+
proposedHeader: proposed.header.toInspect(),
|
|
686
|
+
proposedArchiveRoot: proposed.archive.root.toString(),
|
|
687
|
+
proposedFeeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
|
|
688
|
+
calldataCheckpointNumber: calldataCheckpoint.checkpointNumber,
|
|
689
|
+
calldataHeader: calldataCheckpoint.header.toInspect(),
|
|
690
|
+
calldataArchiveRoot: calldataCheckpoint.archiveRoot.toString(),
|
|
691
|
+
calldataFeeAssetPriceModifier: calldataCheckpoint.feeAssetPriceModifier.toString(),
|
|
692
|
+
},
|
|
693
|
+
);
|
|
694
|
+
// Only emit when the slots match — uncheckpointed entries are pruned above so this
|
|
695
|
+
// should always hold, but guard defensively to avoid mis-attributing a slash.
|
|
696
|
+
if (proposed.header.slotNumber === calldataCheckpoint.header.slotNumber) {
|
|
697
|
+
await this.reportEquivocationIfSameSigner(proposed, calldataCheckpoint);
|
|
698
|
+
}
|
|
699
|
+
// Return a divergence signal so the caller can evict pending >= this number
|
|
700
|
+
return { kind: 'evict', fromCheckpointNumber: proposed.checkpointNumber };
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
this.deps.log.debug(
|
|
704
|
+
`Building published checkpoint from proposed ${calldataCheckpoint.checkpointNumber} (skipping blob fetch)`,
|
|
705
|
+
{ proposedHeader: proposed.header.toInspect(), proposedArchiveRoot: proposed.archive.root.toString() },
|
|
706
|
+
);
|
|
707
|
+
|
|
708
|
+
const blocks = await this.deps.stores.blocks.getBlocks({
|
|
709
|
+
from: BlockNumber(proposed.startBlock),
|
|
710
|
+
limit: proposed.blockCount,
|
|
711
|
+
});
|
|
712
|
+
if (blocks.length !== proposed.blockCount) {
|
|
713
|
+
this.deps.log.warn(
|
|
714
|
+
`Local proposed checkpoint ${proposed.checkpointNumber} has wrong block count (expected ${proposed.blockCount} blocks starting at ${proposed.startBlock} but got ${blocks.length})`,
|
|
715
|
+
{
|
|
716
|
+
proposedCheckpointNumber: proposed.checkpointNumber,
|
|
717
|
+
proposedStartBlock: proposed.startBlock,
|
|
718
|
+
proposedBlockCount: proposed.blockCount,
|
|
719
|
+
retrievedBlocks: blocks.map(b => b.number),
|
|
720
|
+
},
|
|
721
|
+
);
|
|
722
|
+
return { kind: 'none' };
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
const checkpoint = Checkpoint.from({
|
|
726
|
+
archive: proposed.archive,
|
|
727
|
+
header: proposed.header,
|
|
728
|
+
blocks,
|
|
729
|
+
number: proposed.checkpointNumber,
|
|
730
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier,
|
|
731
|
+
});
|
|
732
|
+
const promotedCheckpoint = PublishedCheckpoint.from({
|
|
733
|
+
checkpoint,
|
|
734
|
+
l1: calldataCheckpoint.l1,
|
|
735
|
+
attestations: calldataCheckpoint.attestations,
|
|
736
|
+
verbatimAttestations: calldataCheckpoint.verbatimAttestations,
|
|
737
|
+
});
|
|
738
|
+
this.deps.instrumentation.processCheckpointPromoted();
|
|
739
|
+
|
|
740
|
+
return { kind: 'promote', checkpoint: promotedCheckpoint };
|
|
741
|
+
}
|
|
742
|
+
}
|