@aztec/validator-client 0.0.1-commit.a89ec08 → 0.0.1-commit.aa0c64f

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.
@@ -1,12 +1,19 @@
1
+ import type { Archiver } from '@aztec/archiver';
1
2
  import type { BlobClientInterface } from '@aztec/blob-client/client';
2
3
  import { type Blob, encodeCheckpointBlobDataFromBlocks, getBlobsPerL1Block } from '@aztec/blob-lib';
3
4
  import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
4
5
  import type { EpochCache } from '@aztec/epoch-cache';
5
6
  import { validateFeeAssetPriceModifier } from '@aztec/ethereum/contracts';
6
- import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
7
+ import {
8
+ BlockNumber,
9
+ CheckpointNumber,
10
+ type CheckpointProposalHash,
11
+ SlotNumber,
12
+ } from '@aztec/foundation/branded-types';
7
13
  import { pick } from '@aztec/foundation/collection';
8
14
  import { Fr } from '@aztec/foundation/curves/bn254';
9
15
  import { TimeoutError } from '@aztec/foundation/error';
16
+ import { FifoSet } from '@aztec/foundation/fifo-set';
10
17
  import type { LogData } from '@aztec/foundation/log';
11
18
  import { createLogger } from '@aztec/foundation/log';
12
19
  import { retryUntil } from '@aztec/foundation/retry';
@@ -14,16 +21,23 @@ import { DateProvider, Timer } from '@aztec/foundation/timer';
14
21
  import type { P2P, PeerId } from '@aztec/p2p';
15
22
  import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
16
23
  import type { BlockData, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
17
- import { validateCheckpoint } from '@aztec/stdlib/checkpoint';
18
- import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
24
+ import type { CheckpointReexecutionTracker, ReexecutionOutcome } from '@aztec/stdlib/checkpoint';
25
+ import { getPreviousCheckpointOutHashes, validateCheckpoint } from '@aztec/stdlib/checkpoint';
26
+ import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
19
27
  import { Gas } from '@aztec/stdlib/gas';
20
- import type { ITxProvider, ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
28
+ import type {
29
+ ITxProvider,
30
+ MerkleTreeWriteOperations,
31
+ ValidatorClientFullConfig,
32
+ WorldStateSynchronizer,
33
+ } from '@aztec/stdlib/interfaces/server';
21
34
  import {
22
35
  type L1ToL2MessageSource,
23
36
  accumulateCheckpointOutHashes,
24
37
  computeInHashFromL1ToL2Messages,
25
38
  } from '@aztec/stdlib/messaging';
26
- import type { BlockProposal, CheckpointProposalCore } from '@aztec/stdlib/p2p';
39
+ import type { BlockProposal, CheckpointAttestation, CheckpointProposalCore } from '@aztec/stdlib/p2p';
40
+ import type { ConsensusTimetable } from '@aztec/stdlib/timetable';
27
41
  import { MerkleTreeId } from '@aztec/stdlib/trees';
28
42
  import type { CheckpointGlobalVariables, FailedTx, Tx } from '@aztec/stdlib/tx';
29
43
  import {
@@ -39,9 +53,9 @@ import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
39
53
  import type { ValidatorMetrics } from './metrics.js';
40
54
 
41
55
  export type BlockProposalValidationFailureReason =
56
+ | 'invalid_signature'
42
57
  | 'invalid_proposal'
43
58
  | 'parent_block_not_found'
44
- | 'block_source_not_synced'
45
59
  | 'parent_block_wrong_slot'
46
60
  | 'in_hash_mismatch'
47
61
  | 'global_variables_mismatch'
@@ -51,6 +65,8 @@ export type BlockProposalValidationFailureReason =
51
65
  | 'failed_txs'
52
66
  | 'initial_state_mismatch'
53
67
  | 'timeout'
68
+ | 'block_proposal_beyond_checkpoint'
69
+ | 'checkpoint_proposal_equivocation'
54
70
  | 'unknown_error';
55
71
 
56
72
  type ReexecuteTransactionsResult = {
@@ -75,16 +91,151 @@ export type BlockProposalValidationFailureResult = {
75
91
 
76
92
  export type BlockProposalValidationResult = BlockProposalValidationSuccessResult | BlockProposalValidationFailureResult;
77
93
 
78
- export type CheckpointProposalValidationResult = { isValid: true } | { isValid: false; reason: string };
94
+ export type CheckpointProposalValidationFailureReason =
95
+ | 'invalid_signature'
96
+ | 'invalid_fee_asset_price_modifier'
97
+ | 'last_block_not_found'
98
+ | 'block_fetch_error'
99
+ | 'world_state_not_synced'
100
+ | 'checkpoint_already_published'
101
+ | 'no_blocks_for_slot'
102
+ | 'last_block_archive_mismatch'
103
+ | 'too_many_blocks_in_checkpoint'
104
+ | 'initial_archive_mismatch'
105
+ | 'checkpoint_header_mismatch'
106
+ | 'archive_mismatch'
107
+ | 'out_hash_mismatch'
108
+ | 'checkpoint_validation_failed';
109
+
110
+ /**
111
+ * Mapping from a checkpoint-proposal validation failure reason to the tracker outcome that
112
+ * `handleCheckpointProposal` should record. `undefined` means do not record (signature
113
+ * couldn't be verified, or the checkpoint is already on L1 so the question is moot).
114
+ */
115
+ /* eslint-disable camelcase */
116
+ const CHECKPOINT_VALIDATION_REASON_TO_OUTCOME: Record<
117
+ CheckpointProposalValidationFailureReason,
118
+ ReexecutionOutcome | undefined
119
+ > = {
120
+ invalid_signature: undefined,
121
+ invalid_fee_asset_price_modifier: 'invalid',
122
+ checkpoint_already_published: undefined,
123
+ last_block_not_found: 'unvalidated',
124
+ block_fetch_error: 'unvalidated',
125
+ world_state_not_synced: 'unvalidated',
126
+ initial_archive_mismatch: 'unvalidated',
127
+ no_blocks_for_slot: 'unvalidated',
128
+ last_block_archive_mismatch: 'invalid',
129
+ too_many_blocks_in_checkpoint: 'invalid',
130
+ checkpoint_header_mismatch: 'invalid',
131
+ archive_mismatch: 'invalid',
132
+ out_hash_mismatch: 'invalid',
133
+ checkpoint_validation_failed: 'invalid',
134
+ };
135
+
136
+ export type CheckpointProposalValidationSuccessResult = {
137
+ isValid: true;
138
+ checkpointNumber: CheckpointNumber;
139
+ };
140
+
141
+ export type CheckpointProposalValidationFailureResult = {
142
+ isValid: false;
143
+ reason: CheckpointProposalValidationFailureReason;
144
+ checkpointNumber?: CheckpointNumber;
145
+ };
146
+
147
+ export type CheckpointProposalValidationResult =
148
+ | CheckpointProposalValidationSuccessResult
149
+ | CheckpointProposalValidationFailureResult;
150
+
151
+ export type CheckpointProposalValidationFailureCallback = (
152
+ proposal: CheckpointProposalCore,
153
+ result: CheckpointProposalValidationFailureResult,
154
+ proposalInfo: LogData,
155
+ ) => void | Promise<void>;
79
156
 
80
157
  type CheckpointComputationResult =
81
158
  | { checkpointNumber: CheckpointNumber; reason?: undefined }
82
159
  | { checkpointNumber?: undefined; reason: 'invalid_proposal' | 'global_variables_mismatch' };
83
160
 
84
- /** Handles block and checkpoint proposals for both validator and non-validator nodes. */
161
+ type BlockProposalSlotValidationResult =
162
+ | { isValid: true }
163
+ | { isValid: false; reason: 'block_proposal_beyond_checkpoint' | 'checkpoint_proposal_equivocation' };
164
+
165
+ const MAX_TRACKED_INVALID_PROPOSAL_SLOTS = 1000;
166
+
167
+ /** Block-proposal validation failures that constitute a slashable invalid-block offense. */
168
+ export const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT: BlockProposalValidationFailureReason[] = [
169
+ 'state_mismatch',
170
+ 'failed_txs',
171
+ 'global_variables_mismatch',
172
+ 'invalid_proposal',
173
+ 'parent_block_wrong_slot',
174
+ 'in_hash_mismatch',
175
+ ];
176
+
177
+ /** Checkpoint-proposal validation failures that constitute a slashable invalid-checkpoint offense. */
178
+ export const SLASHABLE_CHECKPOINT_PROPOSAL_VALIDATION_RESULT: Record<
179
+ CheckpointProposalValidationFailureReason,
180
+ boolean
181
+ > = {
182
+ // enabled
183
+ ['invalid_fee_asset_price_modifier']: true,
184
+ ['checkpoint_header_mismatch']: true,
185
+ // These late mismatches should normally be caught by earlier checks, but if reached after validating the local
186
+ // checkpoint inputs, the proposer-signed payload disagrees with deterministic recomputation.
187
+ ['archive_mismatch']: true,
188
+ ['out_hash_mismatch']: true,
189
+ ['no_blocks_for_slot']: true,
190
+ ['too_many_blocks_in_checkpoint']: true,
191
+ ['checkpoint_validation_failed']: true,
192
+ ['last_block_archive_mismatch']: true,
193
+
194
+ // disabled
195
+ ['invalid_signature']: false,
196
+ ['last_block_not_found']: false,
197
+ ['block_fetch_error']: false,
198
+ ['world_state_not_synced']: false,
199
+ // A reorg / divergent local chain, not a proposer offense (mirrors the block path's initial_state_mismatch).
200
+ ['initial_archive_mismatch']: false,
201
+ ['checkpoint_already_published']: false,
202
+ };
203
+
204
+ /**
205
+ * Handles block and checkpoint proposals for both validator and non-validator nodes. Also tracks which slots
206
+ * had a slashable invalid proposal or a proposal equivocation, exposing them via the
207
+ * `InvalidProposalSlotSource` interface consumed by the attested-invalid-proposal slashing watcher. The
208
+ * tracking is populated as a side effect of validating/re-executing proposals, so any node that re-executes
209
+ * proposals (the default) can serve it — not only validators.
210
+ */
85
211
  export class ProposalHandler {
86
212
  public readonly tracer: Tracer;
87
213
 
214
+ /** Cached last checkpoint validation result to avoid double-validation on validator nodes.
215
+ * Keyed by signed-payload hash so two proposals at the same (slot, archive) but with a
216
+ * different `feeAssetPriceModifier` (or any other signed field) are validated independently. */
217
+ private lastCheckpointValidationResult?: {
218
+ payloadHash: CheckpointProposalHash;
219
+ result: CheckpointProposalValidationResult;
220
+ };
221
+
222
+ /** Archiver reference for setting proposed checkpoints (pipelining). Set via register(). */
223
+ private archiver?: Pick<Archiver, 'addProposedCheckpoint' | 'getProposedCheckpointData'>;
224
+
225
+ /** Returns current validator addresses for own-proposal detection. Set via register(). */
226
+ private getOwnValidatorAddresses?: () => string[];
227
+
228
+ /** P2P proposal pool access for deciding when retained proposals should block archiver processing. */
229
+ private p2pClient?: Pick<P2P, 'getProposalsForSlot'>;
230
+
231
+ private checkpointProposalValidationFailureCallback?: CheckpointProposalValidationFailureCallback;
232
+
233
+ /** Slots at which a slashable invalid block or checkpoint proposal was observed. */
234
+ private readonly slotsWithInvalidProposals = FifoSet.withLimit<SlotNumber>(MAX_TRACKED_INVALID_PROPOSAL_SLOTS);
235
+
236
+ /** Slots at which a proposal equivocation was observed; suppresses attested-to-invalid-proposal slashing. */
237
+ private readonly slotsWithProposalEquivocation = FifoSet.withLimit<SlotNumber>(MAX_TRACKED_INVALID_PROPOSAL_SLOTS);
238
+
88
239
  constructor(
89
240
  private checkpointsBuilder: FullNodeCheckpointsBuilder,
90
241
  private worldState: WorldStateSynchronizer,
@@ -93,8 +244,10 @@ export class ProposalHandler {
93
244
  private txProvider: ITxProvider,
94
245
  private blockProposalValidator: BlockProposalValidator,
95
246
  private epochCache: EpochCache,
247
+ private timetable: ConsensusTimetable,
96
248
  private config: ValidatorClientFullConfig,
97
249
  private blobClient: BlobClientInterface,
250
+ private reexecutionTracker: CheckpointReexecutionTracker,
98
251
  private metrics?: ValidatorMetrics,
99
252
  private dateProvider: DateProvider = new DateProvider(),
100
253
  telemetry: TelemetryClient = getTelemetryClient(),
@@ -106,11 +259,67 @@ export class ProposalHandler {
106
259
  this.tracer = telemetry.getTracer('ProposalHandler');
107
260
  }
108
261
 
262
+ public updateConfig(config: Partial<ValidatorClientFullConfig>): void {
263
+ this.config = { ...this.config, ...config };
264
+ }
265
+
266
+ public setCheckpointProposalValidationFailureCallback(callback?: CheckpointProposalValidationFailureCallback): void {
267
+ this.checkpointProposalValidationFailureCallback = callback;
268
+ }
269
+
270
+ /**
271
+ * Records the proposer's own checkpoint proposal as a `valid` outcome in the re-execution
272
+ * tracker. Without this, the node's own checkpoint proposals never flow through
273
+ * `handleCheckpointProposal` (proposers don't validate their own proposals), so its sentinel
274
+ * sees no outcome for slots where it was the proposer and reports itself as inactive.
275
+ *
276
+ * `archive` should be the locally-computed archive (NOT the broadcast archive, which may have
277
+ * been deliberately corrupted in tests via `broadcastInvalidBlockProposal` /
278
+ * `broadcastInvalidCheckpointProposalOnly`). Recording the local archive correctly models the
279
+ * proposer's own view of its own work.
280
+ */
281
+ public recordOwnCheckpointProposalAsValid(slot: SlotNumber, archive: Fr, checkpointNumber: CheckpointNumber): void {
282
+ this.reexecutionTracker.recordOutcome(slot, archive, 'valid', checkpointNumber);
283
+ }
284
+
285
+ /** Whether a slashable invalid block or checkpoint proposal was observed at the given slot (InvalidProposalSlotSource). */
286
+ public hasInvalidProposals(slotNumber: SlotNumber): boolean {
287
+ return this.slotsWithInvalidProposals.has(slotNumber);
288
+ }
289
+
290
+ /** Whether a proposal equivocation was observed at the given slot (InvalidProposalSlotSource). */
291
+ public hasProposalEquivocation(slotNumber: SlotNumber): boolean {
292
+ return this.slotsWithProposalEquivocation.has(slotNumber);
293
+ }
294
+
295
+ /** Records a slot as having a slashable invalid proposal, for offense observers (sentinel/slasher watchers). */
296
+ public markInvalidProposalSlot(slotNumber: SlotNumber): void {
297
+ this.slotsWithInvalidProposals.add(slotNumber);
298
+ }
299
+
300
+ /** Records a slot as having a proposal equivocation, which suppresses attested-to-invalid-proposal slashing. */
301
+ public markProposalEquivocation(slotNumber: SlotNumber): void {
302
+ this.slotsWithProposalEquivocation.add(slotNumber);
303
+ }
304
+
109
305
  /**
110
- * Registers non-validator handlers for block and checkpoint proposals on the p2p client.
111
- * Block proposals are always registered. Checkpoint proposals are registered if the blob client can upload.
306
+ * Registers handlers for block and checkpoint proposals on the p2p client.
307
+ * Records the p2p client so validation can inspect retained proposals.
308
+ * Block proposals are registered for non-validator nodes (validators register their own enhanced handler).
309
+ * The all-nodes checkpoint proposal handler is always registered for validation, caching, and pipelining.
310
+ * @param archiver - Archiver reference for setting proposed checkpoints (pipelining)
311
+ * @param getOwnValidatorAddresses - Returns current validator addresses for own-proposal detection
112
312
  */
113
- register(p2pClient: P2P, shouldReexecute: boolean): ProposalHandler {
313
+ register(
314
+ p2pClient: P2P,
315
+ shouldReexecute: boolean,
316
+ archiver?: Pick<Archiver, 'addProposedCheckpoint' | 'getProposedCheckpointData'>,
317
+ getOwnValidatorAddresses?: () => string[],
318
+ ): ProposalHandler {
319
+ this.p2pClient = p2pClient;
320
+ this.archiver = archiver;
321
+ this.getOwnValidatorAddresses = getOwnValidatorAddresses;
322
+
114
323
  // Non-validator handler that processes or re-executes for monitoring but does not attest.
115
324
  // Returns boolean indicating whether the proposal was valid.
116
325
  const blockHandler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
@@ -128,6 +337,18 @@ export class ProposalHandler {
128
337
  });
129
338
  return true;
130
339
  } else {
340
+ // Track invalid proposals / equivocations so offense observers (the attested-invalid-proposal
341
+ // watcher) work on non-validator nodes too. Validators populate these via their own handlers.
342
+ // Skip invalid-proposal marking while the escape hatch is open, matching the validator path,
343
+ // which intentionally disables invalid-block slashing then.
344
+ if (result.reason === 'checkpoint_proposal_equivocation') {
345
+ this.markProposalEquivocation(slotNumber);
346
+ } else if (
347
+ SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(result.reason) &&
348
+ !(await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber))
349
+ ) {
350
+ this.markInvalidProposalSlot(slotNumber);
351
+ }
131
352
  this.log.warn(
132
353
  `Non-validator block proposal ${blockNumber} at slot ${slotNumber} failed processing with ${result.reason}`,
133
354
  { blockNumber: result.blockNumber, slotNumber, reason: result.reason },
@@ -142,32 +363,79 @@ export class ProposalHandler {
142
363
 
143
364
  p2pClient.registerBlockProposalHandler(blockHandler);
144
365
 
145
- // Register checkpoint proposal handler if blob uploads are enabled and we are reexecuting
146
- if (this.blobClient.canUpload() && shouldReexecute) {
147
- const checkpointHandler = async (checkpoint: CheckpointProposalCore, _sender: PeerId) => {
148
- try {
149
- const proposalInfo = {
150
- proposalSlotNumber: checkpoint.slotNumber,
151
- archive: checkpoint.archive.toString(),
152
- proposer: checkpoint.getSender()?.toString(),
153
- };
154
- const result = await this.handleCheckpointProposal(checkpoint, proposalInfo);
155
- if (result.isValid) {
156
- this.log.info(`Non-validator checkpoint proposal at slot ${checkpoint.slotNumber} handled`, proposalInfo);
157
- } else {
158
- this.log.warn(
159
- `Non-validator checkpoint proposal at slot ${checkpoint.slotNumber} failed: ${result.reason}`,
160
- proposalInfo,
161
- );
366
+ // p2p detects duplicate (equivocated) proposals without routing them through the handlers above, so mark
367
+ // the slot as equivocated here. This suppresses false-positive attested-to-invalid-proposal slashing on
368
+ // non-validator offense collectors. Validators overwrite this with their own richer handler.
369
+ p2pClient.registerDuplicateProposalCallback(info => this.markProposalEquivocation(info.slot));
370
+
371
+ // All-nodes checkpoint proposal handler: validates, caches, and sets proposed checkpoint for pipelining.
372
+ // Runs for all nodes (validators and non-validators). Validators get the cached result in the
373
+ // validator-specific callback (attestToCheckpointProposal) which runs after this one.
374
+ const checkpointHandler = async (
375
+ proposal: CheckpointProposalCore,
376
+ _sender: PeerId,
377
+ ): Promise<CheckpointAttestation[] | undefined> => {
378
+ try {
379
+ const pipeliningTimer = new Timer();
380
+ const proposalInfo: LogData = {
381
+ slot: proposal.slotNumber,
382
+ archive: proposal.archive.toString(),
383
+ proposer: proposal.getSender()?.toString(),
384
+ };
385
+
386
+ if (this.config.skipCheckpointProposalValidation) {
387
+ this.log.warn(`Skipping checkpoint proposal validation for slot ${proposal.slotNumber}`, proposalInfo);
388
+ return undefined;
389
+ }
390
+
391
+ if (await this.epochCache.isEscapeHatchOpenAtSlot(proposal.slotNumber)) {
392
+ this.log.warn(
393
+ `Escape hatch open for slot ${proposal.slotNumber}, skipping checkpoint proposal validation`,
394
+ proposalInfo,
395
+ );
396
+ return undefined;
397
+ }
398
+
399
+ // A proposal is "own" when it was signed by a validator key this node also owns. The true local
400
+ // proposer already built, validated, and stored this checkpoint before broadcasting, so a matching
401
+ // proposed checkpoint is already in its archiver — skip the redundant re-validation. An HA peer that
402
+ // shares the proposer's keys sees the same "own" proposal over gossip but never built it, so it has
403
+ // nothing stored; it falls through to the normal validate-and-persist path below to hydrate the
404
+ // proposed-checkpoint metadata it needs to build the next slot on top of this checkpoint.
405
+ const proposer = proposal.getSender();
406
+ const ownAddresses = this.getOwnValidatorAddresses?.();
407
+ const isOwnProposal = proposer && ownAddresses?.some(addr => addr === proposer.toString());
408
+
409
+ if (isOwnProposal) {
410
+ const existing = await this.archiver?.getProposedCheckpointData({ slot: proposal.slotNumber });
411
+ if (existing?.archive.root.equals(proposal.archive)) {
412
+ this.log.debug(`Skipping sync for existing own checkpoint proposal at slot ${proposal.slotNumber}`);
413
+ return undefined;
162
414
  }
163
- } catch (error) {
164
- this.log.error('Error processing checkpoint proposal in non-validator handler', error);
165
415
  }
166
- // Non-validators don't attest
167
- return undefined;
168
- };
169
- p2pClient.registerCheckpointProposalHandler(checkpointHandler);
170
- }
416
+
417
+ const result = await this.handleCheckpointProposal(proposal, proposalInfo);
418
+ if (!result.isValid) {
419
+ // Track invalid checkpoint proposals so offense observers (the attested-invalid-proposal watcher)
420
+ // work on non-validator nodes too. This handler runs for all nodes; validators also mark via the
421
+ // failure callback below (idempotent).
422
+ if (SLASHABLE_CHECKPOINT_PROPOSAL_VALIDATION_RESULT[result.reason]) {
423
+ this.markInvalidProposalSlot(proposal.slotNumber);
424
+ }
425
+ await this.checkpointProposalValidationFailureCallback?.(proposal, result, proposalInfo);
426
+ } else if (this.archiver) {
427
+ const set = await this.setProposedCheckpoint(proposal);
428
+ if (set) {
429
+ this.metrics?.recordCheckpointProposalToPipelinedStateDuration(pipeliningTimer.ms());
430
+ }
431
+ }
432
+ } catch (err) {
433
+ this.log.warn(`Error handling checkpoint proposal for slot ${proposal.slotNumber}`, { err });
434
+ }
435
+ return undefined;
436
+ };
437
+
438
+ p2pClient.registerAllNodesCheckpointProposalHandler(checkpointHandler);
171
439
 
172
440
  return this;
173
441
  }
@@ -179,12 +447,11 @@ export class ProposalHandler {
179
447
  ): Promise<BlockProposalValidationResult> {
180
448
  const slotNumber = proposal.slotNumber;
181
449
  const proposer = proposal.getSender();
182
- const config = this.checkpointsBuilder.getConfig();
183
450
 
184
451
  // Reject proposals with invalid signatures
185
452
  if (!proposer) {
186
453
  this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
187
- return { isValid: false, reason: 'invalid_proposal' };
454
+ return { isValid: false, reason: 'invalid_signature' };
188
455
  }
189
456
 
190
457
  const proposalInfo = {
@@ -207,17 +474,20 @@ export class ProposalHandler {
207
474
  return { isValid: false, reason: 'invalid_proposal' };
208
475
  }
209
476
 
210
- // Ensure the block source is synced before checking for existing blocks,
211
- // since a pending checkpoint prune may remove blocks we'd otherwise find.
212
- // This affects mostly the block_number_already_exists check, since a pending
213
- // checkpoint prune could remove a block that would conflict with this proposal.
214
- // TODO(@Maddiaa0): This may break staggered slots.
215
- const blockSourceSync = await this.waitForBlockSourceSync(slotNumber);
216
- if (!blockSourceSync) {
217
- this.log.warn(`Block source is not synced, skipping processing`, proposalInfo);
218
- return { isValid: false, reason: 'block_source_not_synced' };
477
+ const retainedSlotValidation = await this.validateNewBlockInSlot(proposal);
478
+ if (!retainedSlotValidation.isValid) {
479
+ this.log.info(`Block proposal conflicts with retained proposals, skipping archiver processing`, {
480
+ ...proposalInfo,
481
+ indexWithinCheckpoint: proposal.indexWithinCheckpoint,
482
+ reason: retainedSlotValidation.reason,
483
+ });
484
+ return { isValid: false, blockNumber: proposal.blockNumber, reason: retainedSlotValidation.reason };
219
485
  }
220
486
 
487
+ // The proposer builds ahead of L1 submission under pipelining, so the block source won't have
488
+ // synced to the proposed slot yet. We deliberately do not wait for it to sync here, to avoid
489
+ // eating into the attestation window.
490
+
221
491
  // Check that the parent proposal is a block we know, otherwise reexecution would fail.
222
492
  // If we don't find it immediately, we keep retrying for a while; it may be we still
223
493
  // need to process other block proposals to get to it.
@@ -244,8 +514,12 @@ export class ProposalHandler {
244
514
  : BlockNumber(parentBlock.header.getBlockNumber() + 1);
245
515
  proposalInfo.blockNumber = blockNumber;
246
516
 
247
- // Check that this block number does not exist already
248
- const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
517
+ // Check that this block number does not exist already. During a reorg the archiver can still hold a
518
+ // stale block at this number (a different archive, about to be pruned) while the proposal carries the
519
+ // rebuilt replacement; resolveExistingBlockAtNumber waits for the local prune in that case so the
520
+ // rebuilt block is processed in time to attest, rather than being permanently dropped on a bare
521
+ // number collision.
522
+ const existingBlock = await this.resolveExistingBlockAtNumber(blockNumber, proposal.archive, slotNumber);
249
523
  if (existingBlock) {
250
524
  this.log.warn(`Block number ${blockNumber} already exists, skipping processing`, proposalInfo);
251
525
  return { isValid: false, blockNumber, reason: 'block_number_already_exists' };
@@ -255,9 +529,12 @@ export class ProposalHandler {
255
529
  // and we do it even if we don't plan to re-execute the txs, so that we have them if another node needs them.
256
530
  const { txs, missingTxs } = await this.txProvider.getTxsForBlockProposal(proposal, blockNumber, {
257
531
  pinnedPeer: proposalSender,
258
- deadline: this.getReexecutionDeadline(slotNumber, config),
532
+ deadline: this.getReexecutionDeadline(slotNumber),
259
533
  });
260
534
 
535
+ // Record the tx-collection outcome on the re-execution tracker
536
+ this.reexecutionTracker.recordTxsCollected(slotNumber, proposal.indexWithinCheckpoint, missingTxs.length === 0);
537
+
261
538
  // If reexecution is disabled, bail. We were just interested in triggering tx collection.
262
539
  if (!shouldReexecute) {
263
540
  this.log.info(
@@ -294,11 +571,18 @@ export class ProposalHandler {
294
571
  return { isValid: false, blockNumber, reason: 'txs_not_available' };
295
572
  }
296
573
 
297
- // Collect the out hashes of all the checkpoints before this one in the same epoch
574
+ // Collect the out hashes of all the checkpoints before this one in the same epoch.
575
+ // Mirror the proposer-side fallback: under pipelining the immediately-preceding cp may not
576
+ // yet be on L1, in which case the helper grafts the locally-known proposed cp's outHash.
298
577
  const epoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
299
- const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
300
- .filter(c => c.checkpointNumber < checkpointNumber)
301
- .map(c => c.checkpointOutHash);
578
+ const previousCheckpointOutHashes = await getPreviousCheckpointOutHashes({
579
+ blockSource: this.blockSource,
580
+ epoch,
581
+ checkpointNumber,
582
+ l1Constants: this.epochCache.getL1Constants(),
583
+ pipeliningEnabled: true,
584
+ log: this.log,
585
+ });
302
586
 
303
587
  // Try re-executing the transactions in the proposal if needed
304
588
  let reexecutionResult;
@@ -319,8 +603,8 @@ export class ProposalHandler {
319
603
  }
320
604
 
321
605
  // If we succeeded, push this block into the archiver (unless disabled)
322
- if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
323
- await this.blockSource.addBlock(reexecutionResult?.block);
606
+ if (reexecutionResult?.block && !this.config.skipPushProposedBlocksToArchiver) {
607
+ await this.blockSource.addBlock(reexecutionResult.block);
324
608
  }
325
609
 
326
610
  this.log.info(
@@ -331,29 +615,47 @@ export class ProposalHandler {
331
615
  return { isValid: true, blockNumber, reexecutionResult };
332
616
  }
333
617
 
618
+ private async validateNewBlockInSlot(blockProposal: BlockProposal): Promise<BlockProposalSlotValidationResult> {
619
+ if (!this.p2pClient) {
620
+ return { isValid: true };
621
+ }
622
+
623
+ const { blockProposals, checkpointProposals } = await this.p2pClient.getProposalsForSlot(blockProposal.slotNumber);
624
+
625
+ if (checkpointProposals.length === 0) {
626
+ return { isValid: true };
627
+ } else if (checkpointProposals.length > 1) {
628
+ return { isValid: false, reason: 'checkpoint_proposal_equivocation' };
629
+ } else {
630
+ const checkpointProposal = checkpointProposals[0];
631
+ const terminalBlock = blockProposals.find(block => block.archive.equals(checkpointProposal.archive));
632
+ return terminalBlock !== undefined && blockProposal.indexWithinCheckpoint > terminalBlock.indexWithinCheckpoint
633
+ ? { isValid: false, reason: 'block_proposal_beyond_checkpoint' }
634
+ : { isValid: true };
635
+ }
636
+ }
637
+
334
638
  private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockData | undefined> {
335
639
  const parentArchive = proposal.blockHeader.lastArchive.root;
336
- const slot = proposal.slotNumber;
337
- const config = this.checkpointsBuilder.getConfig();
338
640
  const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
339
641
 
340
642
  if (parentArchive.equals(genesisArchiveRoot)) {
341
643
  return 'genesis';
342
644
  }
343
645
 
344
- const deadline = this.getReexecutionDeadline(slot, config);
345
- const currentTime = this.dateProvider.now();
346
- const timeoutDurationMs = deadline.getTime() - currentTime;
646
+ const deadline = this.getReexecutionDeadline(proposal.slotNumber);
647
+ const timeoutDurationMs = deadline.getTime() - this.dateProvider.now();
347
648
 
348
649
  try {
349
650
  return (
350
- (await this.blockSource.getBlockDataByArchive(parentArchive)) ??
651
+ (await this.blockSource.getBlockData({ archive: parentArchive })) ??
351
652
  (timeoutDurationMs <= 0
352
653
  ? undefined
353
654
  : await retryUntil(
354
- () => this.blockSource.syncImmediate().then(() => this.blockSource.getBlockDataByArchive(parentArchive)),
655
+ () =>
656
+ this.blockSource.syncImmediate().then(() => this.blockSource.getBlockData({ archive: parentArchive })),
355
657
  'force archiver sync',
356
- timeoutDurationMs / 1000,
658
+ { deadline, dateProvider: this.dateProvider },
357
659
  0.5,
358
660
  ))
359
661
  );
@@ -367,6 +669,63 @@ export class ProposalHandler {
367
669
  }
368
670
  }
369
671
 
672
+ /**
673
+ * Resolves whether a block genuinely already exists at `blockNumber`. Returns the existing block only if
674
+ * it is a true duplicate of the proposal (matching archive). During a reorg the archiver can still hold a
675
+ * stale fork at this number (different archive) that is about to be pruned; in that case this forces L1
676
+ * sync and waits, bounded by the re-execution deadline, for the prune to land, then returns `undefined` so
677
+ * the rebuilt block proposal can be processed in time to attest. If the prune does not complete before the
678
+ * deadline it returns the stale block, so the caller falls back to the safe `block_number_already_exists`
679
+ * rejection.
680
+ */
681
+ private async resolveExistingBlockAtNumber(
682
+ blockNumber: BlockNumber,
683
+ proposalArchive: Fr,
684
+ slotNumber: SlotNumber,
685
+ ): Promise<BlockData | undefined> {
686
+ const existingBlock = await this.blockSource.getBlockData({ number: blockNumber });
687
+ if (!existingBlock || existingBlock.archive.root.equals(proposalArchive)) {
688
+ return existingBlock;
689
+ }
690
+
691
+ // A different block already occupies this number: it may be a stale fork being pruned during a reorg, not a
692
+ // genuine duplicate. Wait for the local prune rather than permanently rejecting the proposal.
693
+ const deadline = this.getReexecutionDeadline(slotNumber);
694
+ if (deadline.getTime() - this.dateProvider.now() <= 0) {
695
+ return existingBlock;
696
+ }
697
+
698
+ this.log.warn(`Block number ${blockNumber} already exists, awaiting potential prune`, {
699
+ blockNumber,
700
+ existingArchive: existingBlock.archive.root.toString(),
701
+ proposalArchive: proposalArchive.toString(),
702
+ });
703
+
704
+ try {
705
+ const { block } = await retryUntil(
706
+ async () => {
707
+ await this.blockSource.syncImmediate();
708
+ const block = await this.blockSource.getBlockData({ number: blockNumber });
709
+ // Resolve once the existing block is gone (pruned) or has been replaced by one matching the
710
+ // proposal — the same condition as the early return above. A matching block is returned so the
711
+ // caller still treats it as a genuine duplicate; an `undefined` (pruned) block lets the proposal
712
+ // be processed. Wrap in an object so the `undefined` case is still a truthy retry result.
713
+ return block === undefined || block.archive.root.equals(proposalArchive) ? { block } : undefined;
714
+ },
715
+ `prune of stale block ${blockNumber}`,
716
+ { deadline, dateProvider: this.dateProvider },
717
+ 0.5,
718
+ );
719
+ return block;
720
+ } catch (err) {
721
+ if (err instanceof TimeoutError) {
722
+ this.log.warn(`Timed out waiting for stale block ${blockNumber} to be pruned`, { blockNumber });
723
+ return existingBlock;
724
+ }
725
+ throw err;
726
+ }
727
+ }
728
+
370
729
  private computeCheckpointNumber(
371
730
  proposal: BlockProposal,
372
731
  parentBlock: 'genesis' | BlockData,
@@ -490,45 +849,14 @@ export class ProposalHandler {
490
849
  return undefined;
491
850
  }
492
851
 
493
- private getReexecutionDeadline(slot: SlotNumber, config: { l1GenesisTime: bigint; slotDuration: number }): Date {
494
- const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
495
- return new Date(nextSlotTimestampSeconds * 1000);
496
- }
497
-
498
- /** Waits for the block source to sync L1 data up to at least the slot before the given one. */
499
- private async waitForBlockSourceSync(slot: SlotNumber): Promise<boolean> {
500
- const deadline = this.getReexecutionDeadline(slot, this.checkpointsBuilder.getConfig());
501
- const timeoutMs = deadline.getTime() - this.dateProvider.now();
502
- if (slot === 0) {
503
- return true;
504
- }
505
-
506
- // Make a quick check before triggering an archiver sync
507
- const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
508
- if (syncedSlot !== undefined && syncedSlot + 1 >= slot) {
509
- return true;
510
- }
511
-
512
- try {
513
- // Trigger an immediate sync of the block source, and wait until it reports being synced to the required slot
514
- return await retryUntil(
515
- async () => {
516
- await this.blockSource.syncImmediate();
517
- const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
518
- return syncedSlot !== undefined && syncedSlot + 1 >= slot;
519
- },
520
- 'wait for block source sync',
521
- timeoutMs / 1000,
522
- 0.5,
523
- );
524
- } catch (err) {
525
- if (err instanceof TimeoutError) {
526
- this.log.warn(`Timed out waiting for block source to sync to slot ${slot}`);
527
- return false;
528
- } else {
529
- throw err;
530
- }
531
- }
852
+ /**
853
+ * Hard re-execution/validation deadline for any block or checkpoint proposal targeting `slotNumber`:
854
+ * the single consensus `attestation_deadline` (`target_slot_start + S - 2E`). This is the latest the
855
+ * checkpoint can land on L1 in the target slot; all nodes agree on it. Loosened from the previous
856
+ * next-wall-clock-slot-boundary bound (see the timetable spec / refactor notes).
857
+ */
858
+ private getReexecutionDeadline(slotNumber: SlotNumber): Date {
859
+ return new Date(this.timetable.getAttestationDeadline(slotNumber) * 1000);
532
860
  }
533
861
 
534
862
  private getReexecuteFailureReason(err: any): BlockProposalValidationFailureReason {
@@ -560,7 +888,7 @@ export class ProposalHandler {
560
888
  // If we do not have all of the transactions, then we should fail
561
889
  if (txs.length !== txHashes.length) {
562
890
  const foundTxHashes = txs.map(tx => tx.getTxHash());
563
- const missingTxHashes = txHashes.filter(txHash => !foundTxHashes.includes(txHash));
891
+ const missingTxHashes = txHashes.filter(txHash => !foundTxHashes.some(h => h.equals(txHash)));
564
892
  throw new TransactionsNotAvailableError(missingTxHashes);
565
893
  }
566
894
 
@@ -608,7 +936,7 @@ export class ProposalHandler {
608
936
  );
609
937
 
610
938
  // Build the new block
611
- const deadline = this.getReexecutionDeadline(slot, config);
939
+ const deadline = this.getReexecutionDeadline(slot);
612
940
  const maxBlockGas =
613
941
  this.config.validateMaxL2BlockGas !== undefined || this.config.validateMaxDABlockGas !== undefined
614
942
  ? new Gas(this.config.validateMaxDABlockGas ?? Infinity, this.config.validateMaxL2BlockGas ?? Infinity)
@@ -672,27 +1000,55 @@ export class ProposalHandler {
672
1000
  }
673
1001
 
674
1002
  /**
675
- * Validates a checkpoint proposal and uploads blobs if configured.
676
- * Used by both non-validator nodes (via register) and the validator client (via delegation).
1003
+ * Validates a checkpoint proposal, caches the result, and uploads blobs if configured.
1004
+ * Returns a cached result if the same proposal (archive + slot) was already validated.
1005
+ * Used by both the all-nodes callback (via register) and the validator client (via delegation).
677
1006
  */
678
1007
  async handleCheckpointProposal(
679
1008
  proposal: CheckpointProposalCore,
680
1009
  proposalInfo: LogData,
681
1010
  ): Promise<CheckpointProposalValidationResult> {
1011
+ const slot = proposal.slotNumber;
1012
+ const payloadHash = proposal.getPayloadHash();
1013
+
1014
+ // Check cache: same signed-payload hash means we already validated this exact proposal.
1015
+ if (this.lastCheckpointValidationResult && this.lastCheckpointValidationResult.payloadHash === payloadHash) {
1016
+ this.log.debug(`Returning cached validation result for checkpoint proposal at slot ${slot}`, proposalInfo);
1017
+ return this.lastCheckpointValidationResult.result;
1018
+ }
1019
+
682
1020
  const proposer = proposal.getSender();
1021
+ let result: CheckpointProposalValidationResult;
683
1022
  if (!proposer) {
684
1023
  this.log.warn(`Received checkpoint proposal with invalid signature for slot ${proposal.slotNumber}`);
685
- return { isValid: false, reason: 'invalid_signature' };
686
- }
687
-
688
- if (!validateFeeAssetPriceModifier(proposal.feeAssetPriceModifier)) {
1024
+ result = { isValid: false as const, reason: 'invalid_signature' };
1025
+ } else if (!validateFeeAssetPriceModifier(proposal.feeAssetPriceModifier)) {
689
1026
  this.log.warn(
690
1027
  `Received checkpoint proposal with invalid feeAssetPriceModifier ${proposal.feeAssetPriceModifier} for slot ${proposal.slotNumber}`,
691
1028
  );
692
- return { isValid: false, reason: 'invalid_fee_asset_price_modifier' };
1029
+ result = { isValid: false, reason: 'invalid_fee_asset_price_modifier' };
1030
+ } else {
1031
+ result = await this.validateCheckpointProposal(proposal, proposalInfo);
693
1032
  }
694
1033
 
695
- const result = await this.validateCheckpointProposal(proposal, proposalInfo);
1034
+ this.lastCheckpointValidationResult = { payloadHash, result };
1035
+
1036
+ // Record the outcome on the re-execution tracker.
1037
+ const outcome = result.isValid ? ('valid' as const) : CHECKPOINT_VALIDATION_REASON_TO_OUTCOME[result.reason];
1038
+ if (outcome !== undefined) {
1039
+ this.reexecutionTracker.recordOutcome(slot, proposal.archive, outcome, result.checkpointNumber);
1040
+ }
1041
+
1042
+ // Drop tracker entries for checkpoints that have reached L1 finality.
1043
+ try {
1044
+ const tips = await this.blockSource.getL2Tips();
1045
+ const finalizedCheckpointNumber = tips.finalized.checkpoint.number;
1046
+ if (finalizedCheckpointNumber > 0) {
1047
+ this.reexecutionTracker.removeBefore(CheckpointNumber(finalizedCheckpointNumber + 1));
1048
+ }
1049
+ } catch (err) {
1050
+ this.log.error(`Error pruning reexecution tracker`, err, proposalInfo);
1051
+ }
696
1052
 
697
1053
  // Upload blobs to filestore if validation passed (fire and forget)
698
1054
  if (result.isValid) {
@@ -712,21 +1068,24 @@ export class ProposalHandler {
712
1068
  ): Promise<CheckpointProposalValidationResult> {
713
1069
  const slot = proposal.slotNumber;
714
1070
 
715
- // Timeout block syncing at the start of the next slot
716
- const config = this.checkpointsBuilder.getConfig();
717
- const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
718
- const timeoutSeconds = Math.max(1, nextSlotTimestampSeconds - Math.floor(this.dateProvider.now() / 1000));
1071
+ // Block-sync/validation deadline = the single consensus attestation_deadline (target_slot_start + S
1072
+ // - 2E): the latest moment the proposer can submit this checkpoint and still have it land on L1 in
1073
+ // the target slot. Keeping validation/attestation alive until then lets validators keep attesting
1074
+ // right up to the proposer's real publish cutoff.
1075
+ const deadline = this.getReexecutionDeadline(slot);
719
1076
 
720
- // Wait for last block to sync by archive
721
- let lastBlockHeader;
1077
+ // Wait for last block to sync by archive. The deadline is passed to retryUntil as an absolute date so
1078
+ // the remaining budget is derived from the date provider; a deadline already in the past times out
1079
+ // after a single attempt instead of looping (the immediate-timeout semantics of the deadline overload).
1080
+ let lastBlockData;
722
1081
  try {
723
- lastBlockHeader = await retryUntil(
1082
+ lastBlockData = await retryUntil(
724
1083
  async () => {
725
1084
  await this.blockSource.syncImmediate();
726
- return this.blockSource.getBlockHeaderByArchive(proposal.archive);
1085
+ return await this.blockSource.getBlockData({ archive: proposal.archive });
727
1086
  },
728
1087
  `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`,
729
- timeoutSeconds,
1088
+ { deadline, dateProvider: this.dateProvider },
730
1089
  0.5,
731
1090
  );
732
1091
  } catch (err) {
@@ -738,22 +1097,55 @@ export class ProposalHandler {
738
1097
  return { isValid: false, reason: 'block_fetch_error' };
739
1098
  }
740
1099
 
741
- if (!lastBlockHeader) {
1100
+ if (!lastBlockData) {
742
1101
  this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
743
1102
  return { isValid: false, reason: 'last_block_not_found' };
744
1103
  }
745
1104
 
1105
+ // Refuse to attest if the block's enclosing checkpoint has already been published to L1.
1106
+ const existingCheckpoint = await this.blockSource.getCheckpointData({ number: lastBlockData.checkpointNumber });
1107
+ if (existingCheckpoint) {
1108
+ this.log.warn(`Refusing to attest to checkpoint proposal whose checkpoint is already on L1`, {
1109
+ ...proposalInfo,
1110
+ checkpointNumber: lastBlockData.checkpointNumber,
1111
+ });
1112
+ return {
1113
+ isValid: false,
1114
+ reason: 'checkpoint_already_published',
1115
+ checkpointNumber: lastBlockData.checkpointNumber,
1116
+ };
1117
+ }
1118
+
746
1119
  // Get all full blocks for the slot and checkpoint
747
1120
  const blocks = await this.blockSource.getBlocksForSlot(slot);
748
1121
  if (blocks.length === 0) {
749
1122
  this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
750
- return { isValid: false, reason: 'no_blocks_for_slot' };
1123
+ return { isValid: false, reason: 'no_blocks_for_slot', checkpointNumber: lastBlockData.checkpointNumber };
751
1124
  }
752
1125
 
753
1126
  // Ensure the last block for this slot matches the archive in the checkpoint proposal
754
1127
  if (!blocks.at(-1)?.archive.root.equals(proposal.archive)) {
755
1128
  this.log.warn(`Last block archive mismatch for checkpoint proposal`, proposalInfo);
756
- return { isValid: false, reason: 'last_block_archive_mismatch' };
1129
+ return {
1130
+ isValid: false,
1131
+ reason: 'last_block_archive_mismatch',
1132
+ checkpointNumber: lastBlockData.checkpointNumber,
1133
+ };
1134
+ }
1135
+
1136
+ // Note this condition should never trigger, since we dont process block proposals that exceed indexWithinCheckpoint
1137
+ const maxBlocksPerCheckpoint = this.config.maxBlocksPerCheckpoint;
1138
+ if (maxBlocksPerCheckpoint !== undefined && blocks.length > maxBlocksPerCheckpoint) {
1139
+ this.log.warn(`Checkpoint proposal exceeds maxBlocksPerCheckpoint`, {
1140
+ ...proposalInfo,
1141
+ blocksInProposal: blocks.length,
1142
+ maxBlocksPerCheckpoint,
1143
+ });
1144
+ return {
1145
+ isValid: false,
1146
+ reason: 'too_many_blocks_in_checkpoint',
1147
+ checkpointNumber: lastBlockData.checkpointNumber,
1148
+ };
757
1149
  }
758
1150
 
759
1151
  this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
@@ -769,87 +1161,122 @@ export class ProposalHandler {
769
1161
  // Get L1-to-L2 messages for this checkpoint
770
1162
  const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
771
1163
 
772
- // Collect the out hashes of all the checkpoints before this one in the same epoch
1164
+ // Collect the out hashes of all the checkpoints before this one in the same epoch.
1165
+ // See note on the analogous block-proposal site: the helper handles pipelining lag.
773
1166
  const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
774
- const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
775
- .filter(c => c.checkpointNumber < checkpointNumber)
776
- .map(c => c.checkpointOutHash);
1167
+ const previousCheckpointOutHashes = await getPreviousCheckpointOutHashes({
1168
+ blockSource: this.blockSource,
1169
+ epoch,
1170
+ checkpointNumber,
1171
+ l1Constants: this.epochCache.getL1Constants(),
1172
+ pipeliningEnabled: true,
1173
+ log: this.log,
1174
+ });
777
1175
 
778
- // Fork world state at the block before the first block
1176
+ // Fork world state at the block before the first block. getFork syncs world state to the parent block
1177
+ // first (see its doc): the block source (archiver) can already hold the block while world state still
1178
+ // trails it by one, and forking a not-yet-applied block throws a raw tree error that would otherwise
1179
+ // escape as an uncaught gossipsub error. We pass the parent's expected block hash so the sync detects a
1180
+ // world-state reorg (undefined for the genesis parent, where no block exists to pin). On failure we map
1181
+ // to a clean validation result rather than letting it escape.
779
1182
  const parentBlockNumber = BlockNumber(firstBlock.number - 1);
780
- const fork = await this.worldState.fork(parentBlockNumber);
781
-
1183
+ let forkResult: MerkleTreeWriteOperations;
782
1184
  try {
783
- // Create checkpoint builder with all existing blocks
784
- const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
785
- checkpointNumber,
786
- constants,
787
- proposal.feeAssetPriceModifier,
788
- l1ToL2Messages,
789
- previousCheckpointOutHashes,
790
- fork,
791
- blocks,
792
- this.log.getBindings(),
793
- );
1185
+ const parentBlockHash = (await this.blockSource.getBlockData({ number: parentBlockNumber }))?.blockHash;
1186
+ forkResult = await this.checkpointsBuilder.getFork(parentBlockNumber, parentBlockHash);
1187
+ } catch (err) {
1188
+ this.log.warn(`Failed to fork world state at block ${parentBlockNumber} for checkpoint proposal`, {
1189
+ ...proposalInfo,
1190
+ parentBlockNumber,
1191
+ err,
1192
+ });
1193
+ return { isValid: false, reason: 'world_state_not_synced', checkpointNumber };
1194
+ }
1195
+ await using fork = forkResult;
794
1196
 
795
- // Complete the checkpoint to get computed values
796
- const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
797
-
798
- // Compare checkpoint header with proposal
799
- if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
800
- this.log.warn(`Checkpoint header mismatch`, {
801
- ...proposalInfo,
802
- computed: computedCheckpoint.header.toInspect(),
803
- proposal: proposal.checkpointHeader.toInspect(),
804
- });
805
- return { isValid: false, reason: 'checkpoint_header_mismatch' };
806
- }
1197
+ // Verify the fork's archive root matches the checkpoint's expected starting archive (the archive after
1198
+ // the parent block). A mismatch means world state forked from a different chain than the proposal was
1199
+ // built on (e.g. a reorg), so recomputing the checkpoint against it would be meaningless. This mirrors
1200
+ // the block-proposal re-execution check and fails fast with a clean, non-slashable result instead of a
1201
+ // confusing downstream mismatch.
1202
+ const forkArchiveRoot = new Fr((await fork.getTreeInfo(MerkleTreeId.ARCHIVE)).root);
1203
+ if (!forkArchiveRoot.equals(proposal.checkpointHeader.lastArchiveRoot)) {
1204
+ this.log.warn(`Fork archive root does not match checkpoint proposal's last archive`, {
1205
+ ...proposalInfo,
1206
+ forkArchiveRoot: forkArchiveRoot.toString(),
1207
+ expectedLastArchiveRoot: proposal.checkpointHeader.lastArchiveRoot.toString(),
1208
+ });
1209
+ return { isValid: false, reason: 'initial_archive_mismatch', checkpointNumber };
1210
+ }
807
1211
 
808
- // Compare archive root with proposal
809
- if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
810
- this.log.warn(`Archive root mismatch`, {
811
- ...proposalInfo,
812
- computed: computedCheckpoint.archive.root.toString(),
813
- proposal: proposal.archive.toString(),
814
- });
815
- return { isValid: false, reason: 'archive_mismatch' };
816
- }
1212
+ // Create checkpoint builder with all existing blocks
1213
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
1214
+ checkpointNumber,
1215
+ constants,
1216
+ proposal.feeAssetPriceModifier,
1217
+ l1ToL2Messages,
1218
+ previousCheckpointOutHashes,
1219
+ fork,
1220
+ blocks,
1221
+ this.log.getBindings(),
1222
+ );
817
1223
 
818
- // Check that the accumulated epoch out hash matches the value in the proposal.
819
- // The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
820
- const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
821
- const computedEpochOutHash = accumulateCheckpointOutHashes([...previousCheckpointOutHashes, checkpointOutHash]);
822
- const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
823
- if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
824
- this.log.warn(`Epoch out hash mismatch`, {
825
- proposalEpochOutHash: proposalEpochOutHash.toString(),
826
- computedEpochOutHash: computedEpochOutHash.toString(),
827
- checkpointOutHash: checkpointOutHash.toString(),
828
- previousCheckpointOutHashes: previousCheckpointOutHashes.map(h => h.toString()),
829
- ...proposalInfo,
830
- });
831
- return { isValid: false, reason: 'out_hash_mismatch' };
832
- }
1224
+ // Complete the checkpoint to get computed values
1225
+ const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
833
1226
 
834
- // Final round of validations on the checkpoint, just in case.
835
- try {
836
- validateCheckpoint(computedCheckpoint, {
837
- rollupManaLimit: this.checkpointsBuilder.getConfig().rollupManaLimit,
838
- maxDABlockGas: this.config.validateMaxDABlockGas,
839
- maxL2BlockGas: this.config.validateMaxL2BlockGas,
840
- maxTxsPerBlock: this.config.validateMaxTxsPerBlock,
841
- maxTxsPerCheckpoint: this.config.validateMaxTxsPerCheckpoint,
842
- });
843
- } catch (err) {
844
- this.log.warn(`Checkpoint validation failed: ${err}`, proposalInfo);
845
- return { isValid: false, reason: 'checkpoint_validation_failed' };
846
- }
1227
+ // Compare checkpoint header with proposal
1228
+ if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
1229
+ this.log.warn(`Checkpoint header mismatch`, {
1230
+ ...proposalInfo,
1231
+ computed: computedCheckpoint.header.toInspect(),
1232
+ proposal: proposal.checkpointHeader.toInspect(),
1233
+ });
1234
+ return { isValid: false, reason: 'checkpoint_header_mismatch', checkpointNumber };
1235
+ }
847
1236
 
848
- this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
849
- return { isValid: true };
850
- } finally {
851
- await fork.close();
1237
+ // Compare archive root with proposal
1238
+ if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
1239
+ this.log.warn(`Archive root mismatch`, {
1240
+ ...proposalInfo,
1241
+ computed: computedCheckpoint.archive.root.toString(),
1242
+ proposal: proposal.archive.toString(),
1243
+ });
1244
+ return { isValid: false, reason: 'archive_mismatch', checkpointNumber };
852
1245
  }
1246
+
1247
+ // Check that the accumulated epoch out hash matches the value in the proposal.
1248
+ // The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
1249
+ const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
1250
+ const computedEpochOutHash = accumulateCheckpointOutHashes([...previousCheckpointOutHashes, checkpointOutHash]);
1251
+ const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
1252
+ if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
1253
+ this.log.warn(`Epoch out hash mismatch`, {
1254
+ proposalEpochOutHash: proposalEpochOutHash.toString(),
1255
+ computedEpochOutHash: computedEpochOutHash.toString(),
1256
+ checkpointOutHash: checkpointOutHash.toString(),
1257
+ previousCheckpointOutHashes: previousCheckpointOutHashes.map(h => h.toString()),
1258
+ ...proposalInfo,
1259
+ });
1260
+ return { isValid: false, reason: 'out_hash_mismatch', checkpointNumber };
1261
+ }
1262
+
1263
+ // Final round of validations on the checkpoint, just in case.
1264
+ try {
1265
+ validateCheckpoint(computedCheckpoint, {
1266
+ rollupManaLimit: this.checkpointsBuilder.getConfig().rollupManaLimit,
1267
+ maxDABlockGas: this.config.validateMaxDABlockGas,
1268
+ maxL2BlockGas: this.config.validateMaxL2BlockGas,
1269
+ maxTxsPerBlock: this.config.validateMaxTxsPerBlock,
1270
+ maxTxsPerCheckpoint: this.config.validateMaxTxsPerCheckpoint,
1271
+ });
1272
+ } catch (err) {
1273
+ this.log.warn(`Checkpoint validation failed: ${err}`, proposalInfo);
1274
+ return { isValid: false, reason: 'checkpoint_validation_failed', checkpointNumber };
1275
+ }
1276
+
1277
+ this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
1278
+
1279
+ return { isValid: true, checkpointNumber };
853
1280
  }
854
1281
 
855
1282
  /** Extracts checkpoint global variables from a block. */
@@ -876,7 +1303,7 @@ export class ProposalHandler {
876
1303
  /** Uploads blobs for a checkpoint to the filestore. */
877
1304
  protected async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
878
1305
  try {
879
- const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
1306
+ const lastBlockHeader = (await this.blockSource.getBlockData({ archive: proposal.archive }))?.header;
880
1307
  if (!lastBlockHeader) {
881
1308
  this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
882
1309
  return;
@@ -900,4 +1327,32 @@ export class ProposalHandler {
900
1327
  this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
901
1328
  }
902
1329
  }
1330
+
1331
+ /**
1332
+ * Derives proposed checkpoint data from validated blocks and sets it on the archiver, so this node can
1333
+ * pipeline building on top of the checkpoint. Does not retry, since validation already waited for the
1334
+ * last block to sync.
1335
+ */
1336
+ private async setProposedCheckpoint(proposal: CheckpointProposalCore): Promise<boolean> {
1337
+ if (!this.archiver) {
1338
+ return false;
1339
+ }
1340
+ const blockData = await this.blockSource.getBlockData({ archive: proposal.archive });
1341
+ if (!blockData) {
1342
+ this.log.debug(`Block data not found for checkpoint proposal archive, cannot set proposed checkpoint`, {
1343
+ archive: proposal.archive.toString(),
1344
+ });
1345
+ return false;
1346
+ }
1347
+
1348
+ await this.archiver.addProposedCheckpoint({
1349
+ header: proposal.checkpointHeader,
1350
+ checkpointNumber: blockData.checkpointNumber,
1351
+ startBlock: BlockNumber(blockData.header.getBlockNumber() - blockData.indexWithinCheckpoint),
1352
+ blockCount: blockData.indexWithinCheckpoint + 1,
1353
+ totalManaUsed: proposal.checkpointHeader.totalManaUsed.toBigInt(),
1354
+ feeAssetPriceModifier: proposal.feeAssetPriceModifier,
1355
+ });
1356
+ return true;
1357
+ }
903
1358
  }