@aztec/validator-client 0.0.1-commit.35158ae7e → 0.0.1-commit.3750d92a7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/factory.ts CHANGED
@@ -9,12 +9,12 @@ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
9
9
  import type { TelemetryClient } from '@aztec/telemetry-client';
10
10
  import type { SlashingProtectionDatabase } from '@aztec/validator-ha-signer/types';
11
11
 
12
- import { BlockProposalHandler } from './block_proposal_handler.js';
13
12
  import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
14
13
  import { ValidatorMetrics } from './metrics.js';
14
+ import { ProposalHandler } from './proposal_handler.js';
15
15
  import { ValidatorClient } from './validator.js';
16
16
 
17
- export function createBlockProposalHandler(
17
+ export function createProposalHandler(
18
18
  config: ValidatorClientFullConfig,
19
19
  deps: {
20
20
  checkpointsBuilder: FullNodeCheckpointsBuilder;
@@ -23,6 +23,7 @@ export function createBlockProposalHandler(
23
23
  l1ToL2MessageSource: L1ToL2MessageSource;
24
24
  p2pClient: P2PClient;
25
25
  epochCache: EpochCache;
26
+ blobClient: BlobClientInterface;
26
27
  dateProvider: DateProvider;
27
28
  telemetry: TelemetryClient;
28
29
  },
@@ -32,7 +33,7 @@ export function createBlockProposalHandler(
32
33
  txsPermitted: !config.disableTransactions,
33
34
  maxTxsPerBlock: config.validateMaxTxsPerBlock ?? config.validateMaxTxsPerCheckpoint,
34
35
  });
35
- return new BlockProposalHandler(
36
+ return new ProposalHandler(
36
37
  deps.checkpointsBuilder,
37
38
  deps.worldState,
38
39
  deps.blockSource,
@@ -41,9 +42,11 @@ export function createBlockProposalHandler(
41
42
  blockProposalValidator,
42
43
  deps.epochCache,
43
44
  config,
45
+ deps.blobClient,
44
46
  metrics,
45
47
  deps.dateProvider,
46
48
  deps.telemetry,
49
+ undefined,
47
50
  );
48
51
  }
49
52
 
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from './block_proposal_handler.js';
1
+ export * from './proposal_handler.js';
2
2
  export * from './checkpoint_builder.js';
3
3
  export * from './config.js';
4
4
  export * from './factory.js';
package/src/metrics.ts CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  createUpDownCounterWithDefault,
12
12
  } from '@aztec/telemetry-client';
13
13
 
14
- import type { BlockProposalValidationFailureReason } from './block_proposal_handler.js';
14
+ import type { BlockProposalValidationFailureReason } from './proposal_handler.js';
15
15
 
16
16
  export class ValidatorMetrics {
17
17
  private failedReexecutionCounter: UpDownCounter;
@@ -24,6 +24,8 @@ export class ValidatorMetrics {
24
24
  private reexMana: Histogram;
25
25
  private reexTx: Histogram;
26
26
  private reexDuration: Gauge;
27
+ private checkpointProposalToPipelinedStateDuration: Histogram;
28
+ private checkpointProposalReceiveOffsetFromNextSlotBoundary: Histogram;
27
29
 
28
30
  constructor(telemetryClient: TelemetryClient) {
29
31
  const meter = telemetryClient.getMeter('Validator');
@@ -77,6 +79,12 @@ export class ValidatorMetrics {
77
79
  this.reexTx = meter.createHistogram(Metrics.VALIDATOR_RE_EXECUTION_TX_COUNT);
78
80
 
79
81
  this.reexDuration = meter.createGauge(Metrics.VALIDATOR_RE_EXECUTION_TIME);
82
+ this.checkpointProposalToPipelinedStateDuration = meter.createHistogram(
83
+ Metrics.VALIDATOR_CHECKPOINT_PROPOSAL_TO_PIPELINED_STATE_DURATION,
84
+ );
85
+ this.checkpointProposalReceiveOffsetFromNextSlotBoundary = meter.createHistogram(
86
+ Metrics.VALIDATOR_CHECKPOINT_PROPOSAL_RECEIVE_OFFSET_FROM_NEXT_SLOT_BOUNDARY,
87
+ );
80
88
  }
81
89
 
82
90
  public recordReex(time: number, txs: number, mManaTotal: number) {
@@ -85,6 +93,16 @@ export class ValidatorMetrics {
85
93
  this.reexMana.record(mManaTotal);
86
94
  }
87
95
 
96
+ public recordCheckpointProposalToPipelinedStateDuration(durationMs: number) {
97
+ this.checkpointProposalToPipelinedStateDuration.record(Math.ceil(durationMs));
98
+ }
99
+
100
+ public recordCheckpointProposalReceiveOffsetFromNextSlotBoundary(offsetMs: number) {
101
+ this.checkpointProposalReceiveOffsetFromNextSlotBoundary.record(Math.ceil(Math.abs(offsetMs)), {
102
+ [Attributes.SLOT_BOUNDARY_SIDE]: offsetMs < 0 ? 'before' : 'after',
103
+ });
104
+ }
105
+
88
106
  public recordFailedReexecution(proposal: BlockProposal) {
89
107
  const proposer = proposal.getSender();
90
108
  this.failedReexecutionCounter.add(1, {
@@ -1,20 +1,30 @@
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';
18
28
  import { MerkleTreeId } from '@aztec/stdlib/trees';
19
29
  import type { CheckpointGlobalVariables, FailedTx, Tx } from '@aztec/stdlib/tx';
20
30
  import {
@@ -66,13 +76,31 @@ export type BlockProposalValidationFailureResult = {
66
76
 
67
77
  export type BlockProposalValidationResult = BlockProposalValidationSuccessResult | BlockProposalValidationFailureResult;
68
78
 
79
+ export type CheckpointProposalValidationResult =
80
+ | { isValid: true; checkpointNumber: CheckpointNumber }
81
+ | { isValid: false; reason: string };
82
+
69
83
  type CheckpointComputationResult =
70
84
  | { checkpointNumber: CheckpointNumber; reason?: undefined }
71
85
  | { checkpointNumber?: undefined; reason: 'invalid_proposal' | 'global_variables_mismatch' };
72
86
 
73
- export class BlockProposalHandler {
87
+ /** Handles block and checkpoint proposals for both validator and non-validator nodes. */
88
+ export class ProposalHandler {
74
89
  public readonly tracer: Tracer;
75
90
 
91
+ /** Cached last checkpoint validation result to avoid double-validation on validator nodes. */
92
+ private lastCheckpointValidationResult?: {
93
+ archive: Fr;
94
+ slotNumber: SlotNumber;
95
+ result: CheckpointProposalValidationResult;
96
+ };
97
+
98
+ /** Archiver reference for setting proposed checkpoints (pipelining). Set via register(). */
99
+ private archiver?: Pick<Archiver, 'setProposedCheckpoint' | 'getL1Constants'>;
100
+
101
+ /** Returns current validator addresses for own-proposal detection. Set via register(). */
102
+ private getOwnValidatorAddresses?: () => string[];
103
+
76
104
  constructor(
77
105
  private checkpointsBuilder: FullNodeCheckpointsBuilder,
78
106
  private worldState: WorldStateSynchronizer,
@@ -82,21 +110,37 @@ export class BlockProposalHandler {
82
110
  private blockProposalValidator: BlockProposalValidator,
83
111
  private epochCache: EpochCache,
84
112
  private config: ValidatorClientFullConfig,
113
+ private blobClient: BlobClientInterface,
85
114
  private metrics?: ValidatorMetrics,
86
115
  private dateProvider: DateProvider = new DateProvider(),
87
116
  telemetry: TelemetryClient = getTelemetryClient(),
88
- private log = createLogger('validator:block-proposal-handler'),
117
+ private log = createLogger('validator:proposal-handler'),
89
118
  ) {
90
119
  if (config.fishermanMode) {
91
120
  this.log = this.log.createChild('[FISHERMAN]');
92
121
  }
93
- this.tracer = telemetry.getTracer('BlockProposalHandler');
122
+ this.tracer = telemetry.getTracer('ProposalHandler');
94
123
  }
95
124
 
96
- register(p2pClient: P2P, shouldReexecute: boolean): BlockProposalHandler {
125
+ /**
126
+ * Registers handlers for block and checkpoint proposals on the p2p client.
127
+ * Block proposals are registered for non-validator nodes (validators register their own enhanced handler).
128
+ * The all-nodes checkpoint proposal handler is always registered for validation, caching, and pipelining.
129
+ * @param archiver - Archiver reference for setting proposed checkpoints (pipelining)
130
+ * @param getOwnValidatorAddresses - Returns current validator addresses for own-proposal detection
131
+ */
132
+ register(
133
+ p2pClient: P2P,
134
+ shouldReexecute: boolean,
135
+ archiver?: Pick<Archiver, 'setProposedCheckpoint' | 'getL1Constants'>,
136
+ getOwnValidatorAddresses?: () => string[],
137
+ ): ProposalHandler {
138
+ this.archiver = archiver;
139
+ this.getOwnValidatorAddresses = getOwnValidatorAddresses;
140
+
97
141
  // Non-validator handler that processes or re-executes for monitoring but does not attest.
98
142
  // Returns boolean indicating whether the proposal was valid.
99
- const handler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
143
+ const blockHandler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
100
144
  try {
101
145
  const { slotNumber, blockNumber } = proposal;
102
146
  const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
@@ -123,7 +167,51 @@ export class BlockProposalHandler {
123
167
  }
124
168
  };
125
169
 
126
- p2pClient.registerBlockProposalHandler(handler);
170
+ p2pClient.registerBlockProposalHandler(blockHandler);
171
+
172
+ // All-nodes checkpoint proposal handler: validates, caches, and sets proposed checkpoint for pipelining.
173
+ // Runs for all nodes (validators and non-validators). Validators get the cached result in the
174
+ // validator-specific callback (attestToCheckpointProposal) which runs after this one.
175
+ const checkpointHandler = async (
176
+ proposal: CheckpointProposalCore,
177
+ _sender: PeerId,
178
+ ): Promise<CheckpointAttestation[] | undefined> => {
179
+ try {
180
+ const pipeliningTimer = new Timer();
181
+ const proposalInfo: LogData = {
182
+ slot: proposal.slotNumber,
183
+ archive: proposal.archive.toString(),
184
+ proposer: proposal.getSender()?.toString(),
185
+ };
186
+
187
+ // For own proposals, skip validation — the proposer already built and validated the checkpoint
188
+ const proposer = proposal.getSender();
189
+ const ownAddresses = this.getOwnValidatorAddresses?.();
190
+ const isOwnProposal = proposer && ownAddresses?.some(addr => addr === proposer.toString());
191
+
192
+ if (isOwnProposal) {
193
+ this.log.debug(`Skipping validation for own checkpoint proposal at slot ${proposal.slotNumber}`);
194
+ if (this.archiver && this.epochCache.isProposerPipeliningEnabled()) {
195
+ await this.setProposedCheckpointFromBlocks(proposal);
196
+ }
197
+ return undefined;
198
+ }
199
+
200
+ const result = await this.handleCheckpointProposal(proposal, proposalInfo);
201
+ if (result.isValid && this.archiver && this.epochCache.isProposerPipeliningEnabled()) {
202
+ const set = await this.setProposedCheckpointFromValidation(proposal);
203
+ if (set) {
204
+ this.metrics?.recordCheckpointProposalToPipelinedStateDuration(pipeliningTimer.ms());
205
+ }
206
+ }
207
+ } catch (err) {
208
+ this.log.warn(`Error handling checkpoint proposal for slot ${proposal.slotNumber}`, { err });
209
+ }
210
+ return undefined;
211
+ };
212
+
213
+ p2pClient.registerAllNodesCheckpointProposalHandler(checkpointHandler);
214
+
127
215
  return this;
128
216
  }
129
217
 
@@ -163,7 +251,7 @@ export class BlockProposalHandler {
163
251
  }
164
252
 
165
253
  // Ensure the block source is synced before checking for existing blocks,
166
- // since a pending checkpoint prune may remove blocks we'd otherwise find.
254
+ // since a proposed checkpoint prune may remove blocks we'd otherwise find.
167
255
  // This affects mostly the block_number_already_exists check, since a pending
168
256
  // checkpoint prune could remove a block that would conflict with this proposal.
169
257
  // When pipelining is enabled, the proposer builds ahead of L1 submission, so the
@@ -279,7 +367,7 @@ export class BlockProposalHandler {
279
367
 
280
368
  // If we succeeded, push this block into the archiver (unless disabled)
281
369
  if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
282
- await this.blockSource.addBlock(reexecutionResult?.block);
370
+ await this.blockSource.addBlock(reexecutionResult.block);
283
371
  }
284
372
 
285
373
  this.log.info(
@@ -292,7 +380,6 @@ export class BlockProposalHandler {
292
380
 
293
381
  private async getParentBlock(proposal: BlockProposal): Promise<'genesis' | BlockData | undefined> {
294
382
  const parentArchive = proposal.blockHeader.lastArchive.root;
295
- const slot = proposal.slotNumber;
296
383
  const config = this.checkpointsBuilder.getConfig();
297
384
  const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
298
385
 
@@ -300,7 +387,7 @@ export class BlockProposalHandler {
300
387
  return 'genesis';
301
388
  }
302
389
 
303
- const deadline = this.getReexecutionDeadline(slot, config);
390
+ const deadline = this.getReexecutionDeadline(proposal.slotNumber, config);
304
391
  const currentTime = this.dateProvider.now();
305
392
  const timeoutDurationMs = deadline.getTime() - currentTime;
306
393
 
@@ -449,8 +536,14 @@ export class BlockProposalHandler {
449
536
  return undefined;
450
537
  }
451
538
 
452
- private getReexecutionDeadline(slot: SlotNumber, config: { l1GenesisTime: bigint; slotDuration: number }): Date {
453
- const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
539
+ private getReexecutionDeadline(
540
+ slotNumber: SlotNumber,
541
+ config: { l1GenesisTime: bigint; slotDuration: number },
542
+ ): Date {
543
+ // Under proposer pipelining, the proposal slot may be ahead of wall clock time.
544
+ // Reexecution budgets should still be bounded by the current slot we are in now.
545
+ const wallclockSlot = slotNumber - this.epochCache.pipeliningOffset();
546
+ const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(wallclockSlot + 1), config));
454
547
  return new Date(nextSlotTimestampSeconds * 1000);
455
548
  }
456
549
 
@@ -463,8 +556,9 @@ export class BlockProposalHandler {
463
556
  }
464
557
 
465
558
  // Make a quick check before triggering an archiver sync
559
+ // If we are pipelining and have a pending checkpoint number stored, we will allow the block proposal to be for a slot further
466
560
  const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
467
- if (syncedSlot !== undefined && syncedSlot + 1 >= slot) {
561
+ if (syncedSlot !== undefined && syncedSlot + 1 + this.epochCache.pipeliningOffset() >= slot) {
468
562
  return true;
469
563
  }
470
564
 
@@ -473,8 +567,8 @@ export class BlockProposalHandler {
473
567
  return await retryUntil(
474
568
  async () => {
475
569
  await this.blockSource.syncImmediate();
476
- const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
477
- return syncedSlot !== undefined && syncedSlot + 1 >= slot;
570
+ const updatedSyncedSlot = await this.blockSource.getSyncedL2SlotNumber();
571
+ return updatedSyncedSlot !== undefined && updatedSyncedSlot + 1 >= slot;
478
572
  },
479
573
  'wait for block source sync',
480
574
  timeoutMs / 1000,
@@ -629,4 +723,320 @@ export class BlockProposalHandler {
629
723
  totalManaUsed,
630
724
  };
631
725
  }
726
+
727
+ /**
728
+ * Validates a checkpoint proposal, caches the result, and uploads blobs if configured.
729
+ * Returns a cached result if the same proposal (archive + slot) was already validated.
730
+ * Used by both the all-nodes callback (via register) and the validator client (via delegation).
731
+ */
732
+ async handleCheckpointProposal(
733
+ proposal: CheckpointProposalCore,
734
+ proposalInfo: LogData,
735
+ ): Promise<CheckpointProposalValidationResult> {
736
+ const slot = proposal.slotNumber;
737
+
738
+ // Check cache: same archive+slot means we already validated this proposal
739
+ if (
740
+ this.lastCheckpointValidationResult &&
741
+ this.lastCheckpointValidationResult.archive.equals(proposal.archive) &&
742
+ this.lastCheckpointValidationResult.slotNumber === slot
743
+ ) {
744
+ this.log.debug(`Returning cached validation result for checkpoint proposal at slot ${slot}`, proposalInfo);
745
+ return this.lastCheckpointValidationResult.result;
746
+ }
747
+
748
+ const proposer = proposal.getSender();
749
+ if (!proposer) {
750
+ this.log.warn(`Received checkpoint proposal with invalid signature for slot ${proposal.slotNumber}`);
751
+ const result: CheckpointProposalValidationResult = { isValid: false, reason: 'invalid_signature' };
752
+ this.lastCheckpointValidationResult = { archive: proposal.archive, slotNumber: slot, result };
753
+ return result;
754
+ }
755
+
756
+ if (!validateFeeAssetPriceModifier(proposal.feeAssetPriceModifier)) {
757
+ this.log.warn(
758
+ `Received checkpoint proposal with invalid feeAssetPriceModifier ${proposal.feeAssetPriceModifier} for slot ${proposal.slotNumber}`,
759
+ );
760
+ const result: CheckpointProposalValidationResult = { isValid: false, reason: 'invalid_fee_asset_price_modifier' };
761
+ this.lastCheckpointValidationResult = { archive: proposal.archive, slotNumber: slot, result };
762
+ return result;
763
+ }
764
+
765
+ const result = await this.validateCheckpointProposal(proposal, proposalInfo);
766
+ this.lastCheckpointValidationResult = { archive: proposal.archive, slotNumber: slot, result };
767
+
768
+ // Upload blobs to filestore if validation passed (fire and forget)
769
+ if (result.isValid) {
770
+ this.tryUploadBlobsForCheckpoint(proposal, proposalInfo);
771
+ }
772
+
773
+ return result;
774
+ }
775
+
776
+ /**
777
+ * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
778
+ * @returns Validation result with isValid flag and reason if invalid.
779
+ */
780
+ async validateCheckpointProposal(
781
+ proposal: CheckpointProposalCore,
782
+ proposalInfo: LogData,
783
+ ): Promise<CheckpointProposalValidationResult> {
784
+ const slot = proposal.slotNumber;
785
+
786
+ // Timeout block syncing at the start of the next slot
787
+ const config = this.checkpointsBuilder.getConfig();
788
+ const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
789
+ const timeoutSeconds = Math.max(1, nextSlotTimestampSeconds - Math.floor(this.dateProvider.now() / 1000));
790
+
791
+ // Wait for last block to sync by archive
792
+ let lastBlockHeader;
793
+ try {
794
+ lastBlockHeader = await retryUntil(
795
+ async () => {
796
+ await this.blockSource.syncImmediate();
797
+ return this.blockSource.getBlockHeaderByArchive(proposal.archive);
798
+ },
799
+ `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`,
800
+ timeoutSeconds,
801
+ 0.5,
802
+ );
803
+ } catch (err) {
804
+ if (err instanceof TimeoutError) {
805
+ this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
806
+ return { isValid: false, reason: 'last_block_not_found' };
807
+ }
808
+ this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
809
+ return { isValid: false, reason: 'block_fetch_error' };
810
+ }
811
+
812
+ if (!lastBlockHeader) {
813
+ this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
814
+ return { isValid: false, reason: 'last_block_not_found' };
815
+ }
816
+
817
+ // Get all full blocks for the slot and checkpoint
818
+ const blocks = await this.blockSource.getBlocksForSlot(slot);
819
+ if (blocks.length === 0) {
820
+ this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
821
+ return { isValid: false, reason: 'no_blocks_for_slot' };
822
+ }
823
+
824
+ // Ensure the last block for this slot matches the archive in the checkpoint proposal
825
+ if (!blocks.at(-1)?.archive.root.equals(proposal.archive)) {
826
+ this.log.warn(`Last block archive mismatch for checkpoint proposal`, proposalInfo);
827
+ return { isValid: false, reason: 'last_block_archive_mismatch' };
828
+ }
829
+
830
+ this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
831
+ ...proposalInfo,
832
+ blockNumbers: blocks.map(b => b.number),
833
+ });
834
+
835
+ // Get checkpoint constants from first block
836
+ const firstBlock = blocks[0];
837
+ const constants = this.extractCheckpointConstants(firstBlock);
838
+ const checkpointNumber = firstBlock.checkpointNumber;
839
+
840
+ // Get L1-to-L2 messages for this checkpoint
841
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
842
+
843
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
844
+ const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
845
+ const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
846
+ .filter(c => c.checkpointNumber < checkpointNumber)
847
+ .map(c => c.checkpointOutHash);
848
+
849
+ // Fork world state at the block before the first block
850
+ const parentBlockNumber = BlockNumber(firstBlock.number - 1);
851
+ await using fork = await this.checkpointsBuilder.getFork(parentBlockNumber);
852
+
853
+ // Create checkpoint builder with all existing blocks
854
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
855
+ checkpointNumber,
856
+ constants,
857
+ proposal.feeAssetPriceModifier,
858
+ l1ToL2Messages,
859
+ previousCheckpointOutHashes,
860
+ fork,
861
+ blocks,
862
+ this.log.getBindings(),
863
+ );
864
+
865
+ // Complete the checkpoint to get computed values
866
+ const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
867
+
868
+ // Compare checkpoint header with proposal
869
+ if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
870
+ this.log.warn(`Checkpoint header mismatch`, {
871
+ ...proposalInfo,
872
+ computed: computedCheckpoint.header.toInspect(),
873
+ proposal: proposal.checkpointHeader.toInspect(),
874
+ });
875
+ return { isValid: false, reason: 'checkpoint_header_mismatch' };
876
+ }
877
+
878
+ // Compare archive root with proposal
879
+ if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
880
+ this.log.warn(`Archive root mismatch`, {
881
+ ...proposalInfo,
882
+ computed: computedCheckpoint.archive.root.toString(),
883
+ proposal: proposal.archive.toString(),
884
+ });
885
+ return { isValid: false, reason: 'archive_mismatch' };
886
+ }
887
+
888
+ // Check that the accumulated epoch out hash matches the value in the proposal.
889
+ // The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
890
+ const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
891
+ const computedEpochOutHash = accumulateCheckpointOutHashes([...previousCheckpointOutHashes, checkpointOutHash]);
892
+ const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
893
+ if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
894
+ this.log.warn(`Epoch out hash mismatch`, {
895
+ proposalEpochOutHash: proposalEpochOutHash.toString(),
896
+ computedEpochOutHash: computedEpochOutHash.toString(),
897
+ checkpointOutHash: checkpointOutHash.toString(),
898
+ previousCheckpointOutHashes: previousCheckpointOutHashes.map(h => h.toString()),
899
+ ...proposalInfo,
900
+ });
901
+ return { isValid: false, reason: 'out_hash_mismatch' };
902
+ }
903
+
904
+ // Final round of validations on the checkpoint, just in case.
905
+ try {
906
+ validateCheckpoint(computedCheckpoint, {
907
+ rollupManaLimit: this.checkpointsBuilder.getConfig().rollupManaLimit,
908
+ maxDABlockGas: this.config.validateMaxDABlockGas,
909
+ maxL2BlockGas: this.config.validateMaxL2BlockGas,
910
+ maxTxsPerBlock: this.config.validateMaxTxsPerBlock,
911
+ maxTxsPerCheckpoint: this.config.validateMaxTxsPerCheckpoint,
912
+ });
913
+ } catch (err) {
914
+ this.log.warn(`Checkpoint validation failed: ${err}`, proposalInfo);
915
+ return { isValid: false, reason: 'checkpoint_validation_failed' };
916
+ }
917
+
918
+ this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
919
+ return { isValid: true, checkpointNumber };
920
+ }
921
+
922
+ /** Extracts checkpoint global variables from a block. */
923
+ private extractCheckpointConstants(block: L2Block): CheckpointGlobalVariables {
924
+ const gv = block.header.globalVariables;
925
+ return {
926
+ chainId: gv.chainId,
927
+ version: gv.version,
928
+ slotNumber: gv.slotNumber,
929
+ timestamp: gv.timestamp,
930
+ coinbase: gv.coinbase,
931
+ feeRecipient: gv.feeRecipient,
932
+ gasFees: gv.gasFees,
933
+ };
934
+ }
935
+
936
+ /** Triggers blob upload for a checkpoint if the blob client can upload (fire and forget). */
937
+ protected tryUploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): void {
938
+ if (this.blobClient.canUpload()) {
939
+ void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
940
+ }
941
+ }
942
+
943
+ /** Uploads blobs for a checkpoint to the filestore. */
944
+ protected async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
945
+ try {
946
+ const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
947
+ if (!lastBlockHeader) {
948
+ this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
949
+ return;
950
+ }
951
+
952
+ const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
953
+ if (blocks.length === 0) {
954
+ this.log.warn(`No blocks found for blob upload`, proposalInfo);
955
+ return;
956
+ }
957
+
958
+ const blockBlobData = blocks.map(b => b.toBlockBlobData());
959
+ const blobFields = encodeCheckpointBlobDataFromBlocks(blockBlobData);
960
+ const blobs: Blob[] = await getBlobsPerL1Block(blobFields);
961
+ await this.blobClient.sendBlobsToFilestore(blobs);
962
+ this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
963
+ ...proposalInfo,
964
+ numBlobs: blobs.length,
965
+ });
966
+ } catch (err) {
967
+ this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
968
+ }
969
+ }
970
+
971
+ /**
972
+ * Derives proposed checkpoint data from validated blocks and sets it on the archiver.
973
+ * Used after successful validation of a foreign proposal.
974
+ * Does not retry since we already waited for the block during validation.
975
+ */
976
+ private async setProposedCheckpointFromValidation(proposal: CheckpointProposalCore): Promise<boolean> {
977
+ if (!this.archiver) {
978
+ return false;
979
+ }
980
+ const blockData = await this.blockSource.getBlockDataByArchive(proposal.archive);
981
+ if (!blockData) {
982
+ this.log.debug(`Block data not found for checkpoint proposal archive, cannot set proposed checkpoint`, {
983
+ archive: proposal.archive.toString(),
984
+ });
985
+ return false;
986
+ }
987
+
988
+ await this.archiver.setProposedCheckpoint({
989
+ header: proposal.checkpointHeader,
990
+ checkpointNumber: blockData.checkpointNumber,
991
+ startBlock: BlockNumber(blockData.header.getBlockNumber() - blockData.indexWithinCheckpoint),
992
+ blockCount: blockData.indexWithinCheckpoint + 1,
993
+ totalManaUsed: proposal.checkpointHeader.totalManaUsed.toBigInt(),
994
+ feeAssetPriceModifier: proposal.feeAssetPriceModifier,
995
+ });
996
+ return true;
997
+ }
998
+
999
+ /**
1000
+ * Sets proposed checkpoint from blocks for own proposals (skips full validation).
1001
+ * Retries fetching block data since the checkpoint proposal often arrives before the last block
1002
+ * finishes re-execution.
1003
+ */
1004
+ private async setProposedCheckpointFromBlocks(proposal: CheckpointProposalCore): Promise<boolean> {
1005
+ if (!this.archiver) {
1006
+ return false;
1007
+ }
1008
+ let blockData = await this.blockSource.getBlockDataByArchive(proposal.archive);
1009
+
1010
+ if (!blockData) {
1011
+ // The checkpoint proposal often arrives before the last block finishes re-execution.
1012
+ // Retry until we find the data or give up at the end of the slot.
1013
+ const nextSlot = this.epochCache.getSlotNow() + 1;
1014
+ const timeOfNextSlot = getTimestampForSlot(SlotNumber(nextSlot), await this.archiver.getL1Constants());
1015
+ const timeoutSeconds = Math.max(1, Number(timeOfNextSlot) - Math.floor(this.dateProvider.now() / 1000));
1016
+
1017
+ blockData = await retryUntil(
1018
+ () => this.blockSource.getBlockDataByArchive(proposal.archive),
1019
+ 'block data for own checkpoint proposal',
1020
+ timeoutSeconds,
1021
+ 0.25,
1022
+ ).catch(() => undefined);
1023
+ }
1024
+
1025
+ if (blockData) {
1026
+ await this.archiver.setProposedCheckpoint({
1027
+ header: proposal.checkpointHeader,
1028
+ checkpointNumber: blockData.checkpointNumber,
1029
+ startBlock: BlockNumber(blockData.header.getBlockNumber() - blockData.indexWithinCheckpoint),
1030
+ blockCount: blockData.indexWithinCheckpoint + 1,
1031
+ totalManaUsed: proposal.checkpointHeader.totalManaUsed.toBigInt(),
1032
+ feeAssetPriceModifier: proposal.feeAssetPriceModifier,
1033
+ });
1034
+ return true;
1035
+ } else {
1036
+ this.log.debug(`Block data not found for own checkpoint proposal archive, cannot set proposed checkpoint`, {
1037
+ archive: proposal.archive.toString(),
1038
+ });
1039
+ return false;
1040
+ }
1041
+ }
632
1042
  }