@aztec/validator-client 0.0.1-commit.ef17749e1 → 0.0.1-commit.f1b29a41e

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,23 +1,35 @@
1
+ import type { Archiver } from '@aztec/archiver';
2
+ import type { BlobClientInterface } from '@aztec/blob-client/client';
3
+ import { type Blob, encodeCheckpointBlobDataFromBlocks, getBlobsPerL1Block } from '@aztec/blob-lib';
1
4
  import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
5
  import type { EpochCache } from '@aztec/epoch-cache';
6
+ import { validateFeeAssetPriceModifier } from '@aztec/ethereum/contracts';
3
7
  import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
4
8
  import { pick } from '@aztec/foundation/collection';
5
9
  import { Fr } from '@aztec/foundation/curves/bn254';
6
10
  import { TimeoutError } from '@aztec/foundation/error';
11
+ import type { LogData } from '@aztec/foundation/log';
7
12
  import { createLogger } from '@aztec/foundation/log';
8
13
  import { retryUntil } from '@aztec/foundation/retry';
9
14
  import { DateProvider, Timer } from '@aztec/foundation/timer';
10
15
  import type { P2P, PeerId } from '@aztec/p2p';
11
16
  import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
12
17
  import type { BlockData, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
18
+ import { validateCheckpoint } from '@aztec/stdlib/checkpoint';
13
19
  import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
14
20
  import { Gas } from '@aztec/stdlib/gas';
15
21
  import type { ITxProvider, ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
16
- import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
17
- import type { BlockProposal } from '@aztec/stdlib/p2p';
22
+ import {
23
+ type L1ToL2MessageSource,
24
+ accumulateCheckpointOutHashes,
25
+ computeInHashFromL1ToL2Messages,
26
+ } from '@aztec/stdlib/messaging';
27
+ import type { BlockProposal, CheckpointAttestation, CheckpointProposalCore } from '@aztec/stdlib/p2p';
28
+ import { MerkleTreeId } from '@aztec/stdlib/trees';
18
29
  import type { CheckpointGlobalVariables, FailedTx, Tx } from '@aztec/stdlib/tx';
19
30
  import {
20
31
  ReExFailedTxsError,
32
+ ReExInitialStateMismatchError,
21
33
  ReExStateMismatchError,
22
34
  ReExTimeoutError,
23
35
  TransactionsNotAvailableError,
@@ -30,6 +42,7 @@ import type { ValidatorMetrics } from './metrics.js';
30
42
  export type BlockProposalValidationFailureReason =
31
43
  | 'invalid_proposal'
32
44
  | 'parent_block_not_found'
45
+ | 'block_source_not_synced'
33
46
  | 'parent_block_wrong_slot'
34
47
  | 'in_hash_mismatch'
35
48
  | 'global_variables_mismatch'
@@ -37,6 +50,7 @@ export type BlockProposalValidationFailureReason =
37
50
  | 'txs_not_available'
38
51
  | 'state_mismatch'
39
52
  | 'failed_txs'
53
+ | 'initial_state_mismatch'
40
54
  | 'timeout'
41
55
  | 'unknown_error';
42
56
 
@@ -62,13 +76,29 @@ export type BlockProposalValidationFailureResult = {
62
76
 
63
77
  export type BlockProposalValidationResult = BlockProposalValidationSuccessResult | BlockProposalValidationFailureResult;
64
78
 
79
+ export type CheckpointProposalValidationResult = { isValid: true } | { isValid: false; reason: string };
80
+
65
81
  type CheckpointComputationResult =
66
82
  | { checkpointNumber: CheckpointNumber; reason?: undefined }
67
83
  | { checkpointNumber?: undefined; reason: 'invalid_proposal' | 'global_variables_mismatch' };
68
84
 
69
- export class BlockProposalHandler {
85
+ /** Handles block and checkpoint proposals for both validator and non-validator nodes. */
86
+ export class ProposalHandler {
70
87
  public readonly tracer: Tracer;
71
88
 
89
+ /** Cached last checkpoint validation result to avoid double-validation on validator nodes. */
90
+ private lastCheckpointValidationResult?: {
91
+ archive: Fr;
92
+ slotNumber: SlotNumber;
93
+ result: CheckpointProposalValidationResult;
94
+ };
95
+
96
+ /** Archiver reference for setting proposed checkpoints (pipelining). Set via register(). */
97
+ private archiver?: Pick<Archiver, 'setProposedCheckpoint' | 'getL1Constants'>;
98
+
99
+ /** Returns current validator addresses for own-proposal detection. Set via register(). */
100
+ private getOwnValidatorAddresses?: () => string[];
101
+
72
102
  constructor(
73
103
  private checkpointsBuilder: FullNodeCheckpointsBuilder,
74
104
  private worldState: WorldStateSynchronizer,
@@ -78,21 +108,37 @@ export class BlockProposalHandler {
78
108
  private blockProposalValidator: BlockProposalValidator,
79
109
  private epochCache: EpochCache,
80
110
  private config: ValidatorClientFullConfig,
111
+ private blobClient: BlobClientInterface,
81
112
  private metrics?: ValidatorMetrics,
82
113
  private dateProvider: DateProvider = new DateProvider(),
83
114
  telemetry: TelemetryClient = getTelemetryClient(),
84
- private log = createLogger('validator:block-proposal-handler'),
115
+ private log = createLogger('validator:proposal-handler'),
85
116
  ) {
86
117
  if (config.fishermanMode) {
87
118
  this.log = this.log.createChild('[FISHERMAN]');
88
119
  }
89
- this.tracer = telemetry.getTracer('BlockProposalHandler');
120
+ this.tracer = telemetry.getTracer('ProposalHandler');
90
121
  }
91
122
 
92
- register(p2pClient: P2P, shouldReexecute: boolean): BlockProposalHandler {
123
+ /**
124
+ * Registers handlers for block and checkpoint proposals on the p2p client.
125
+ * Block proposals are registered for non-validator nodes (validators register their own enhanced handler).
126
+ * The all-nodes checkpoint proposal handler is always registered for validation, caching, and pipelining.
127
+ * @param archiver - Archiver reference for setting proposed checkpoints (pipelining)
128
+ * @param getOwnValidatorAddresses - Returns current validator addresses for own-proposal detection
129
+ */
130
+ register(
131
+ p2pClient: P2P,
132
+ shouldReexecute: boolean,
133
+ archiver?: Pick<Archiver, 'setProposedCheckpoint' | 'getL1Constants'>,
134
+ getOwnValidatorAddresses?: () => string[],
135
+ ): ProposalHandler {
136
+ this.archiver = archiver;
137
+ this.getOwnValidatorAddresses = getOwnValidatorAddresses;
138
+
93
139
  // Non-validator handler that processes or re-executes for monitoring but does not attest.
94
140
  // Returns boolean indicating whether the proposal was valid.
95
- const handler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
141
+ const blockHandler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
96
142
  try {
97
143
  const { slotNumber, blockNumber } = proposal;
98
144
  const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
@@ -119,7 +165,47 @@ export class BlockProposalHandler {
119
165
  }
120
166
  };
121
167
 
122
- p2pClient.registerBlockProposalHandler(handler);
168
+ p2pClient.registerBlockProposalHandler(blockHandler);
169
+
170
+ // All-nodes checkpoint proposal handler: validates, caches, and sets proposed checkpoint for pipelining.
171
+ // Runs for all nodes (validators and non-validators). Validators get the cached result in the
172
+ // validator-specific callback (attestToCheckpointProposal) which runs after this one.
173
+ const checkpointHandler = async (
174
+ proposal: CheckpointProposalCore,
175
+ _sender: PeerId,
176
+ ): Promise<CheckpointAttestation[] | undefined> => {
177
+ try {
178
+ const proposalInfo: LogData = {
179
+ slot: proposal.slotNumber,
180
+ archive: proposal.archive.toString(),
181
+ proposer: proposal.getSender()?.toString(),
182
+ };
183
+
184
+ // For own proposals, skip validation — the proposer already built and validated the checkpoint
185
+ const proposer = proposal.getSender();
186
+ const ownAddresses = this.getOwnValidatorAddresses?.();
187
+ const isOwnProposal = proposer && ownAddresses?.some(addr => addr === proposer.toString());
188
+
189
+ if (isOwnProposal) {
190
+ this.log.debug(`Skipping validation for own checkpoint proposal at slot ${proposal.slotNumber}`);
191
+ if (this.archiver && this.epochCache.isProposerPipeliningEnabled()) {
192
+ await this.setProposedCheckpointFromBlocks(proposal);
193
+ }
194
+ return undefined;
195
+ }
196
+
197
+ const result = await this.handleCheckpointProposal(proposal, proposalInfo);
198
+ if (result.isValid && this.archiver && this.epochCache.isProposerPipeliningEnabled()) {
199
+ await this.setProposedCheckpointFromValidation(proposal);
200
+ }
201
+ } catch (err) {
202
+ this.log.warn(`Error handling checkpoint proposal for slot ${proposal.slotNumber}`, { err });
203
+ }
204
+ return undefined;
205
+ };
206
+
207
+ p2pClient.registerAllNodesCheckpointProposalHandler(checkpointHandler);
208
+
123
209
  return this;
124
210
  }
125
211
 
@@ -138,7 +224,13 @@ export class BlockProposalHandler {
138
224
  return { isValid: false, reason: 'invalid_proposal' };
139
225
  }
140
226
 
141
- const proposalInfo = { ...proposal.toBlockInfo(), proposer: proposer.toString() };
227
+ const proposalInfo = {
228
+ ...proposal.toBlockInfo(),
229
+ proposer: proposer.toString(),
230
+ blockNumber: undefined as BlockNumber | undefined,
231
+ checkpointNumber: undefined as CheckpointNumber | undefined,
232
+ };
233
+
142
234
  this.log.info(`Processing proposal for slot ${slotNumber}`, {
143
235
  ...proposalInfo,
144
236
  txHashes: proposal.txHashes.map(t => t.toString()),
@@ -152,7 +244,24 @@ export class BlockProposalHandler {
152
244
  return { isValid: false, reason: 'invalid_proposal' };
153
245
  }
154
246
 
155
- // Check that the parent proposal is a block we know, otherwise reexecution would fail
247
+ // Ensure the block source is synced before checking for existing blocks,
248
+ // since a proposed checkpoint prune may remove blocks we'd otherwise find.
249
+ // This affects mostly the block_number_already_exists check, since a pending
250
+ // checkpoint prune could remove a block that would conflict with this proposal.
251
+ // When pipelining is enabled, the proposer builds ahead of L1 submission, so the
252
+ // block source won't have synced to the proposed slot yet. Skip the sync wait to
253
+ // avoid eating into the attestation window.
254
+ if (!this.epochCache.isProposerPipeliningEnabled()) {
255
+ const blockSourceSync = await this.waitForBlockSourceSync(slotNumber);
256
+ if (!blockSourceSync) {
257
+ this.log.warn(`Block source is not synced, skipping processing`, proposalInfo);
258
+ return { isValid: false, reason: 'block_source_not_synced' };
259
+ }
260
+ }
261
+
262
+ // Check that the parent proposal is a block we know, otherwise reexecution would fail.
263
+ // If we don't find it immediately, we keep retrying for a while; it may be we still
264
+ // need to process other block proposals to get to it.
156
265
  const parentBlock = await this.getParentBlock(proposal);
157
266
  if (parentBlock === undefined) {
158
267
  this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
@@ -174,6 +283,7 @@ export class BlockProposalHandler {
174
283
  parentBlock === 'genesis'
175
284
  ? BlockNumber(INITIAL_L2_BLOCK_NUM)
176
285
  : BlockNumber(parentBlock.header.getBlockNumber() + 1);
286
+ proposalInfo.blockNumber = blockNumber;
177
287
 
178
288
  // Check that this block number does not exist already
179
289
  const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
@@ -189,7 +299,7 @@ export class BlockProposalHandler {
189
299
  deadline: this.getReexecutionDeadline(slotNumber, config),
190
300
  });
191
301
 
192
- // If reexecution is disabled, bail. We are just interested in triggering tx collection.
302
+ // If reexecution is disabled, bail. We were just interested in triggering tx collection.
193
303
  if (!shouldReexecute) {
194
304
  this.log.info(
195
305
  `Received valid block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`,
@@ -204,6 +314,7 @@ export class BlockProposalHandler {
204
314
  return { isValid: false, blockNumber, reason: checkpointResult.reason };
205
315
  }
206
316
  const checkpointNumber = checkpointResult.checkpointNumber;
317
+ proposalInfo.checkpointNumber = checkpointNumber;
207
318
 
208
319
  // Check that I have the same set of l1ToL2Messages as the proposal
209
320
  const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
@@ -250,7 +361,7 @@ export class BlockProposalHandler {
250
361
 
251
362
  // If we succeeded, push this block into the archiver (unless disabled)
252
363
  if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
253
- await this.blockSource.addBlock(reexecutionResult?.block);
364
+ await this.blockSource.addBlock(reexecutionResult.block);
254
365
  }
255
366
 
256
367
  this.log.info(
@@ -425,8 +536,48 @@ export class BlockProposalHandler {
425
536
  return new Date(nextSlotTimestampSeconds * 1000);
426
537
  }
427
538
 
428
- private getReexecuteFailureReason(err: any) {
429
- if (err instanceof ReExStateMismatchError) {
539
+ /** Waits for the block source to sync L1 data up to at least the slot before the given one. */
540
+ private async waitForBlockSourceSync(slot: SlotNumber): Promise<boolean> {
541
+ const deadline = this.getReexecutionDeadline(slot, this.checkpointsBuilder.getConfig());
542
+ const timeoutMs = deadline.getTime() - this.dateProvider.now();
543
+ if (slot === 0) {
544
+ return true;
545
+ }
546
+
547
+ // Make a quick check before triggering an archiver sync
548
+ const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
549
+ if (syncedSlot !== undefined && syncedSlot + 1 >= slot) {
550
+ return true;
551
+ }
552
+
553
+ try {
554
+ // Trigger an immediate sync of the block source, and wait until it reports being synced to the required slot
555
+ return await retryUntil(
556
+ async () => {
557
+ await this.blockSource.syncImmediate();
558
+ const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
559
+ return syncedSlot !== undefined && syncedSlot + 1 >= slot;
560
+ },
561
+ 'wait for block source sync',
562
+ timeoutMs / 1000,
563
+ 0.5,
564
+ );
565
+ } catch (err) {
566
+ if (err instanceof TimeoutError) {
567
+ this.log.warn(`Timed out waiting for block source to sync to slot ${slot}`);
568
+ return false;
569
+ } else {
570
+ throw err;
571
+ }
572
+ }
573
+ }
574
+
575
+ private getReexecuteFailureReason(err: any): BlockProposalValidationFailureReason {
576
+ if (err instanceof TransactionsNotAvailableError) {
577
+ return 'txs_not_available';
578
+ } else if (err instanceof ReExInitialStateMismatchError) {
579
+ return 'initial_state_mismatch';
580
+ } else if (err instanceof ReExStateMismatchError) {
430
581
  return 'state_mismatch';
431
582
  } else if (err instanceof ReExFailedTxsError) {
432
583
  return 'failed_txs';
@@ -467,6 +618,13 @@ export class BlockProposalHandler {
467
618
  await this.worldState.syncImmediate(parentBlockNumber);
468
619
  await using fork = await this.worldState.fork(parentBlockNumber);
469
620
 
621
+ // Verify the fork's archive root matches the proposal's expected last archive.
622
+ // If they don't match, our world state synced to a different chain and reexecution would fail.
623
+ const forkArchiveRoot = new Fr((await fork.getTreeInfo(MerkleTreeId.ARCHIVE)).root);
624
+ if (!forkArchiveRoot.equals(proposal.blockHeader.lastArchive.root)) {
625
+ throw new ReExInitialStateMismatchError(proposal.blockHeader.lastArchive.root, forkArchiveRoot);
626
+ }
627
+
470
628
  // Build checkpoint constants from proposal (excludes blockNumber which is per-block)
471
629
  const constants: CheckpointGlobalVariables = {
472
630
  chainId: new Fr(config.l1ChainId),
@@ -497,6 +655,8 @@ export class BlockProposalHandler {
497
655
  ? new Gas(this.config.validateMaxDABlockGas ?? Infinity, this.config.validateMaxL2BlockGas ?? Infinity)
498
656
  : undefined;
499
657
  const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
658
+ isBuildingProposal: false,
659
+ minValidTxs: 0,
500
660
  deadline,
501
661
  expectedEndState: blockHeader.state,
502
662
  maxTransactions: this.config.validateMaxTxsPerBlock,
@@ -551,4 +711,317 @@ export class BlockProposalHandler {
551
711
  totalManaUsed,
552
712
  };
553
713
  }
714
+
715
+ /**
716
+ * Validates a checkpoint proposal, caches the result, and uploads blobs if configured.
717
+ * Returns a cached result if the same proposal (archive + slot) was already validated.
718
+ * Used by both the all-nodes callback (via register) and the validator client (via delegation).
719
+ */
720
+ async handleCheckpointProposal(
721
+ proposal: CheckpointProposalCore,
722
+ proposalInfo: LogData,
723
+ ): Promise<CheckpointProposalValidationResult> {
724
+ const slot = proposal.slotNumber;
725
+
726
+ // Check cache: same archive+slot means we already validated this proposal
727
+ if (
728
+ this.lastCheckpointValidationResult &&
729
+ this.lastCheckpointValidationResult.archive.equals(proposal.archive) &&
730
+ this.lastCheckpointValidationResult.slotNumber === slot
731
+ ) {
732
+ this.log.debug(`Returning cached validation result for checkpoint proposal at slot ${slot}`, proposalInfo);
733
+ return this.lastCheckpointValidationResult.result;
734
+ }
735
+
736
+ const proposer = proposal.getSender();
737
+ if (!proposer) {
738
+ this.log.warn(`Received checkpoint proposal with invalid signature for slot ${proposal.slotNumber}`);
739
+ const result: CheckpointProposalValidationResult = { isValid: false, reason: 'invalid_signature' };
740
+ this.lastCheckpointValidationResult = { archive: proposal.archive, slotNumber: slot, result };
741
+ return result;
742
+ }
743
+
744
+ if (!validateFeeAssetPriceModifier(proposal.feeAssetPriceModifier)) {
745
+ this.log.warn(
746
+ `Received checkpoint proposal with invalid feeAssetPriceModifier ${proposal.feeAssetPriceModifier} for slot ${proposal.slotNumber}`,
747
+ );
748
+ const result: CheckpointProposalValidationResult = { isValid: false, reason: 'invalid_fee_asset_price_modifier' };
749
+ this.lastCheckpointValidationResult = { archive: proposal.archive, slotNumber: slot, result };
750
+ return result;
751
+ }
752
+
753
+ const result = await this.validateCheckpointProposal(proposal, proposalInfo);
754
+ this.lastCheckpointValidationResult = { archive: proposal.archive, slotNumber: slot, result };
755
+
756
+ // Upload blobs to filestore if validation passed (fire and forget)
757
+ if (result.isValid) {
758
+ this.tryUploadBlobsForCheckpoint(proposal, proposalInfo);
759
+ }
760
+
761
+ return result;
762
+ }
763
+
764
+ /**
765
+ * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
766
+ * @returns Validation result with isValid flag and reason if invalid.
767
+ */
768
+ async validateCheckpointProposal(
769
+ proposal: CheckpointProposalCore,
770
+ proposalInfo: LogData,
771
+ ): Promise<CheckpointProposalValidationResult> {
772
+ const slot = proposal.slotNumber;
773
+
774
+ // Timeout block syncing at the start of the next slot
775
+ const config = this.checkpointsBuilder.getConfig();
776
+ const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
777
+ const timeoutSeconds = Math.max(1, nextSlotTimestampSeconds - Math.floor(this.dateProvider.now() / 1000));
778
+
779
+ // Wait for last block to sync by archive
780
+ let lastBlockHeader;
781
+ try {
782
+ lastBlockHeader = await retryUntil(
783
+ async () => {
784
+ await this.blockSource.syncImmediate();
785
+ return this.blockSource.getBlockHeaderByArchive(proposal.archive);
786
+ },
787
+ `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`,
788
+ timeoutSeconds,
789
+ 0.5,
790
+ );
791
+ } catch (err) {
792
+ if (err instanceof TimeoutError) {
793
+ this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
794
+ return { isValid: false, reason: 'last_block_not_found' };
795
+ }
796
+ this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
797
+ return { isValid: false, reason: 'block_fetch_error' };
798
+ }
799
+
800
+ if (!lastBlockHeader) {
801
+ this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
802
+ return { isValid: false, reason: 'last_block_not_found' };
803
+ }
804
+
805
+ // Get all full blocks for the slot and checkpoint
806
+ const blocks = await this.blockSource.getBlocksForSlot(slot);
807
+ if (blocks.length === 0) {
808
+ this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
809
+ return { isValid: false, reason: 'no_blocks_for_slot' };
810
+ }
811
+
812
+ // Ensure the last block for this slot matches the archive in the checkpoint proposal
813
+ if (!blocks.at(-1)?.archive.root.equals(proposal.archive)) {
814
+ this.log.warn(`Last block archive mismatch for checkpoint proposal`, proposalInfo);
815
+ return { isValid: false, reason: 'last_block_archive_mismatch' };
816
+ }
817
+
818
+ this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
819
+ ...proposalInfo,
820
+ blockNumbers: blocks.map(b => b.number),
821
+ });
822
+
823
+ // Get checkpoint constants from first block
824
+ const firstBlock = blocks[0];
825
+ const constants = this.extractCheckpointConstants(firstBlock);
826
+ const checkpointNumber = firstBlock.checkpointNumber;
827
+
828
+ // Get L1-to-L2 messages for this checkpoint
829
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
830
+
831
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
832
+ const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
833
+ const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
834
+ .filter(c => c.checkpointNumber < checkpointNumber)
835
+ .map(c => c.checkpointOutHash);
836
+
837
+ // Fork world state at the block before the first block
838
+ const parentBlockNumber = BlockNumber(firstBlock.number - 1);
839
+ await using fork = await this.checkpointsBuilder.getFork(parentBlockNumber);
840
+
841
+ // Create checkpoint builder with all existing blocks
842
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
843
+ checkpointNumber,
844
+ constants,
845
+ proposal.feeAssetPriceModifier,
846
+ l1ToL2Messages,
847
+ previousCheckpointOutHashes,
848
+ fork,
849
+ blocks,
850
+ this.log.getBindings(),
851
+ );
852
+
853
+ // Complete the checkpoint to get computed values
854
+ const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
855
+
856
+ // Compare checkpoint header with proposal
857
+ if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
858
+ this.log.warn(`Checkpoint header mismatch`, {
859
+ ...proposalInfo,
860
+ computed: computedCheckpoint.header.toInspect(),
861
+ proposal: proposal.checkpointHeader.toInspect(),
862
+ });
863
+ return { isValid: false, reason: 'checkpoint_header_mismatch' };
864
+ }
865
+
866
+ // Compare archive root with proposal
867
+ if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
868
+ this.log.warn(`Archive root mismatch`, {
869
+ ...proposalInfo,
870
+ computed: computedCheckpoint.archive.root.toString(),
871
+ proposal: proposal.archive.toString(),
872
+ });
873
+ return { isValid: false, reason: 'archive_mismatch' };
874
+ }
875
+
876
+ // Check that the accumulated epoch out hash matches the value in the proposal.
877
+ // The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
878
+ const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
879
+ const computedEpochOutHash = accumulateCheckpointOutHashes([...previousCheckpointOutHashes, checkpointOutHash]);
880
+ const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
881
+ if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
882
+ this.log.warn(`Epoch out hash mismatch`, {
883
+ proposalEpochOutHash: proposalEpochOutHash.toString(),
884
+ computedEpochOutHash: computedEpochOutHash.toString(),
885
+ checkpointOutHash: checkpointOutHash.toString(),
886
+ previousCheckpointOutHashes: previousCheckpointOutHashes.map(h => h.toString()),
887
+ ...proposalInfo,
888
+ });
889
+ return { isValid: false, reason: 'out_hash_mismatch' };
890
+ }
891
+
892
+ // Final round of validations on the checkpoint, just in case.
893
+ try {
894
+ validateCheckpoint(computedCheckpoint, {
895
+ rollupManaLimit: this.checkpointsBuilder.getConfig().rollupManaLimit,
896
+ maxDABlockGas: this.config.validateMaxDABlockGas,
897
+ maxL2BlockGas: this.config.validateMaxL2BlockGas,
898
+ maxTxsPerBlock: this.config.validateMaxTxsPerBlock,
899
+ maxTxsPerCheckpoint: this.config.validateMaxTxsPerCheckpoint,
900
+ });
901
+ } catch (err) {
902
+ this.log.warn(`Checkpoint validation failed: ${err}`, proposalInfo);
903
+ return { isValid: false, reason: 'checkpoint_validation_failed' };
904
+ }
905
+
906
+ this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
907
+ return { isValid: true };
908
+ }
909
+
910
+ /** Extracts checkpoint global variables from a block. */
911
+ private extractCheckpointConstants(block: L2Block): CheckpointGlobalVariables {
912
+ const gv = block.header.globalVariables;
913
+ return {
914
+ chainId: gv.chainId,
915
+ version: gv.version,
916
+ slotNumber: gv.slotNumber,
917
+ timestamp: gv.timestamp,
918
+ coinbase: gv.coinbase,
919
+ feeRecipient: gv.feeRecipient,
920
+ gasFees: gv.gasFees,
921
+ };
922
+ }
923
+
924
+ /** Triggers blob upload for a checkpoint if the blob client can upload (fire and forget). */
925
+ protected tryUploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): void {
926
+ if (this.blobClient.canUpload()) {
927
+ void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
928
+ }
929
+ }
930
+
931
+ /** Uploads blobs for a checkpoint to the filestore. */
932
+ protected async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
933
+ try {
934
+ const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
935
+ if (!lastBlockHeader) {
936
+ this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
937
+ return;
938
+ }
939
+
940
+ const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
941
+ if (blocks.length === 0) {
942
+ this.log.warn(`No blocks found for blob upload`, proposalInfo);
943
+ return;
944
+ }
945
+
946
+ const blockBlobData = blocks.map(b => b.toBlockBlobData());
947
+ const blobFields = encodeCheckpointBlobDataFromBlocks(blockBlobData);
948
+ const blobs: Blob[] = await getBlobsPerL1Block(blobFields);
949
+ await this.blobClient.sendBlobsToFilestore(blobs);
950
+ this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
951
+ ...proposalInfo,
952
+ numBlobs: blobs.length,
953
+ });
954
+ } catch (err) {
955
+ this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
956
+ }
957
+ }
958
+
959
+ /**
960
+ * Derives proposed checkpoint data from validated blocks and sets it on the archiver.
961
+ * Used after successful validation of a foreign proposal.
962
+ * Does not retry since we already waited for the block during validation.
963
+ */
964
+ private async setProposedCheckpointFromValidation(proposal: CheckpointProposalCore): Promise<void> {
965
+ if (!this.archiver) {
966
+ return;
967
+ }
968
+ const blockData = await this.blockSource.getBlockDataByArchive(proposal.archive);
969
+ if (!blockData) {
970
+ this.log.debug(`Block data not found for checkpoint proposal archive, cannot set proposed checkpoint`, {
971
+ archive: proposal.archive.toString(),
972
+ });
973
+ return;
974
+ }
975
+
976
+ await this.archiver.setProposedCheckpoint({
977
+ header: proposal.checkpointHeader,
978
+ checkpointNumber: blockData.checkpointNumber,
979
+ startBlock: BlockNumber(blockData.header.getBlockNumber() - blockData.indexWithinCheckpoint),
980
+ blockCount: blockData.indexWithinCheckpoint + 1,
981
+ totalManaUsed: proposal.checkpointHeader.totalManaUsed.toBigInt(),
982
+ feeAssetPriceModifier: proposal.feeAssetPriceModifier,
983
+ });
984
+ }
985
+
986
+ /**
987
+ * Sets proposed checkpoint from blocks for own proposals (skips full validation).
988
+ * Retries fetching block data since the checkpoint proposal often arrives before the last block
989
+ * finishes re-execution.
990
+ */
991
+ private async setProposedCheckpointFromBlocks(proposal: CheckpointProposalCore): Promise<void> {
992
+ if (!this.archiver) {
993
+ return;
994
+ }
995
+ let blockData = await this.blockSource.getBlockDataByArchive(proposal.archive);
996
+
997
+ if (!blockData) {
998
+ // The checkpoint proposal often arrives before the last block finishes re-execution.
999
+ // Retry until we find the data or give up at the end of the slot.
1000
+ const nextSlot = this.epochCache.getSlotNow() + 1;
1001
+ const timeOfNextSlot = getTimestampForSlot(SlotNumber(nextSlot), await this.archiver.getL1Constants());
1002
+ const timeoutSeconds = Math.max(1, Number(timeOfNextSlot) - Math.floor(this.dateProvider.now() / 1000));
1003
+
1004
+ blockData = await retryUntil(
1005
+ () => this.blockSource.getBlockDataByArchive(proposal.archive),
1006
+ 'block data for own checkpoint proposal',
1007
+ timeoutSeconds,
1008
+ 0.25,
1009
+ ).catch(() => undefined);
1010
+ }
1011
+
1012
+ if (blockData) {
1013
+ await this.archiver.setProposedCheckpoint({
1014
+ header: proposal.checkpointHeader,
1015
+ checkpointNumber: blockData.checkpointNumber,
1016
+ startBlock: BlockNumber(blockData.header.getBlockNumber() - blockData.indexWithinCheckpoint),
1017
+ blockCount: blockData.indexWithinCheckpoint + 1,
1018
+ totalManaUsed: proposal.checkpointHeader.totalManaUsed.toBigInt(),
1019
+ feeAssetPriceModifier: proposal.feeAssetPriceModifier,
1020
+ });
1021
+ } else {
1022
+ this.log.debug(`Block data not found for own checkpoint proposal archive, cannot set proposed checkpoint`, {
1023
+ archive: proposal.archive.toString(),
1024
+ });
1025
+ }
1026
+ }
554
1027
  }