@aztec/validator-client 0.0.1-commit.9ef841308 → 0.0.1-commit.a4600f49

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