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

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.
Files changed (39) hide show
  1. package/README.md +9 -12
  2. package/dest/checkpoint_builder.d.ts +10 -7
  3. package/dest/checkpoint_builder.d.ts.map +1 -1
  4. package/dest/checkpoint_builder.js +64 -41
  5. package/dest/config.d.ts +1 -1
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +4 -9
  8. package/dest/duties/validation_service.d.ts +7 -9
  9. package/dest/duties/validation_service.d.ts.map +1 -1
  10. package/dest/duties/validation_service.js +14 -26
  11. package/dest/factory.d.ts +7 -4
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +5 -5
  14. package/dest/index.d.ts +2 -2
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +1 -1
  17. package/dest/key_store/ha_key_store.js +1 -1
  18. package/dest/metrics.d.ts +6 -2
  19. package/dest/metrics.d.ts.map +1 -1
  20. package/dest/metrics.js +12 -0
  21. package/dest/proposal_handler.d.ts +108 -0
  22. package/dest/proposal_handler.d.ts.map +1 -0
  23. package/dest/{block_proposal_handler.js → proposal_handler.js} +440 -17
  24. package/dest/validator.d.ts +15 -20
  25. package/dest/validator.d.ts.map +1 -1
  26. package/dest/validator.js +60 -228
  27. package/package.json +19 -19
  28. package/src/checkpoint_builder.ts +79 -52
  29. package/src/config.ts +4 -9
  30. package/src/duties/validation_service.ts +17 -30
  31. package/src/factory.ts +10 -4
  32. package/src/index.ts +1 -1
  33. package/src/key_store/ha_key_store.ts +1 -1
  34. package/src/metrics.ts +19 -1
  35. package/src/proposal_handler.ts +1042 -0
  36. package/src/validator.ts +88 -251
  37. package/dest/block_proposal_handler.d.ts +0 -63
  38. package/dest/block_proposal_handler.d.ts.map +0 -1
  39. package/src/block_proposal_handler.ts +0 -554
@@ -63,7 +63,9 @@ function _ts_dispose_resources(env) {
63
63
  return next();
64
64
  })(env);
65
65
  }
66
+ import { encodeCheckpointBlobDataFromBlocks, getBlobsPerL1Block } from '@aztec/blob-lib';
66
67
  import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
68
+ import { validateFeeAssetPriceModifier } from '@aztec/ethereum/contracts';
67
69
  import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
68
70
  import { pick } from '@aztec/foundation/collection';
69
71
  import { Fr } from '@aztec/foundation/curves/bn254';
@@ -71,12 +73,14 @@ import { TimeoutError } from '@aztec/foundation/error';
71
73
  import { createLogger } from '@aztec/foundation/log';
72
74
  import { retryUntil } from '@aztec/foundation/retry';
73
75
  import { DateProvider, Timer } from '@aztec/foundation/timer';
76
+ import { validateCheckpoint } from '@aztec/stdlib/checkpoint';
74
77
  import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
75
78
  import { Gas } from '@aztec/stdlib/gas';
76
- import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
77
- import { ReExFailedTxsError, ReExStateMismatchError, ReExTimeoutError, TransactionsNotAvailableError } from '@aztec/stdlib/validators';
79
+ import { accumulateCheckpointOutHashes, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
80
+ import { MerkleTreeId } from '@aztec/stdlib/trees';
81
+ import { ReExFailedTxsError, ReExInitialStateMismatchError, ReExStateMismatchError, ReExTimeoutError, TransactionsNotAvailableError } from '@aztec/stdlib/validators';
78
82
  import { getTelemetryClient } from '@aztec/telemetry-client';
79
- export class BlockProposalHandler {
83
+ /** Handles block and checkpoint proposals for both validator and non-validator nodes. */ export class ProposalHandler {
80
84
  checkpointsBuilder;
81
85
  worldState;
82
86
  blockSource;
@@ -85,11 +89,15 @@ export class BlockProposalHandler {
85
89
  blockProposalValidator;
86
90
  epochCache;
87
91
  config;
92
+ blobClient;
88
93
  metrics;
89
94
  dateProvider;
90
95
  log;
91
96
  tracer;
92
- constructor(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, epochCache, config, metrics, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator:block-proposal-handler')){
97
+ /** Cached last checkpoint validation result to avoid double-validation on validator nodes. */ lastCheckpointValidationResult;
98
+ /** Archiver reference for setting proposed checkpoints (pipelining). Set via register(). */ archiver;
99
+ /** Returns current validator addresses for own-proposal detection. Set via register(). */ getOwnValidatorAddresses;
100
+ constructor(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, epochCache, config, blobClient, metrics, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator:proposal-handler')){
93
101
  this.checkpointsBuilder = checkpointsBuilder;
94
102
  this.worldState = worldState;
95
103
  this.blockSource = blockSource;
@@ -98,18 +106,27 @@ export class BlockProposalHandler {
98
106
  this.blockProposalValidator = blockProposalValidator;
99
107
  this.epochCache = epochCache;
100
108
  this.config = config;
109
+ this.blobClient = blobClient;
101
110
  this.metrics = metrics;
102
111
  this.dateProvider = dateProvider;
103
112
  this.log = log;
104
113
  if (config.fishermanMode) {
105
114
  this.log = this.log.createChild('[FISHERMAN]');
106
115
  }
107
- this.tracer = telemetry.getTracer('BlockProposalHandler');
116
+ this.tracer = telemetry.getTracer('ProposalHandler');
108
117
  }
109
- register(p2pClient, shouldReexecute) {
118
+ /**
119
+ * Registers handlers for block and checkpoint proposals on the p2p client.
120
+ * Block proposals are registered for non-validator nodes (validators register their own enhanced handler).
121
+ * The all-nodes checkpoint proposal handler is always registered for validation, caching, and pipelining.
122
+ * @param archiver - Archiver reference for setting proposed checkpoints (pipelining)
123
+ * @param getOwnValidatorAddresses - Returns current validator addresses for own-proposal detection
124
+ */ register(p2pClient, shouldReexecute, archiver, getOwnValidatorAddresses) {
125
+ this.archiver = archiver;
126
+ this.getOwnValidatorAddresses = getOwnValidatorAddresses;
110
127
  // Non-validator handler that processes or re-executes for monitoring but does not attest.
111
128
  // Returns boolean indicating whether the proposal was valid.
112
- const handler = async (proposal, proposalSender)=>{
129
+ const blockHandler = async (proposal, proposalSender)=>{
113
130
  try {
114
131
  const { slotNumber, blockNumber } = proposal;
115
132
  const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
@@ -136,7 +153,44 @@ export class BlockProposalHandler {
136
153
  return false;
137
154
  }
138
155
  };
139
- p2pClient.registerBlockProposalHandler(handler);
156
+ p2pClient.registerBlockProposalHandler(blockHandler);
157
+ // All-nodes checkpoint proposal handler: validates, caches, and sets proposed checkpoint for pipelining.
158
+ // Runs for all nodes (validators and non-validators). Validators get the cached result in the
159
+ // validator-specific callback (attestToCheckpointProposal) which runs after this one.
160
+ const checkpointHandler = async (proposal, _sender)=>{
161
+ try {
162
+ const pipeliningTimer = new Timer();
163
+ const proposalInfo = {
164
+ slot: proposal.slotNumber,
165
+ archive: proposal.archive.toString(),
166
+ proposer: proposal.getSender()?.toString()
167
+ };
168
+ // For own proposals, skip validation — the proposer already built and validated the checkpoint
169
+ const proposer = proposal.getSender();
170
+ const ownAddresses = this.getOwnValidatorAddresses?.();
171
+ const isOwnProposal = proposer && ownAddresses?.some((addr)=>addr === proposer.toString());
172
+ if (isOwnProposal) {
173
+ this.log.debug(`Skipping validation for own checkpoint proposal at slot ${proposal.slotNumber}`);
174
+ if (this.archiver && this.epochCache.isProposerPipeliningEnabled()) {
175
+ await this.setProposedCheckpointFromBlocks(proposal);
176
+ }
177
+ return undefined;
178
+ }
179
+ const result = await this.handleCheckpointProposal(proposal, proposalInfo);
180
+ if (result.isValid && this.archiver && this.epochCache.isProposerPipeliningEnabled()) {
181
+ const set = await this.setProposedCheckpointFromValidation(proposal);
182
+ if (set) {
183
+ this.metrics?.recordCheckpointProposalToPipelinedStateDuration(pipeliningTimer.ms());
184
+ }
185
+ }
186
+ } catch (err) {
187
+ this.log.warn(`Error handling checkpoint proposal for slot ${proposal.slotNumber}`, {
188
+ err
189
+ });
190
+ }
191
+ return undefined;
192
+ };
193
+ p2pClient.registerAllNodesCheckpointProposalHandler(checkpointHandler);
140
194
  return this;
141
195
  }
142
196
  async handleBlockProposal(proposal, proposalSender, shouldReexecute) {
@@ -153,7 +207,9 @@ export class BlockProposalHandler {
153
207
  }
154
208
  const proposalInfo = {
155
209
  ...proposal.toBlockInfo(),
156
- proposer: proposer.toString()
210
+ proposer: proposer.toString(),
211
+ blockNumber: undefined,
212
+ checkpointNumber: undefined
157
213
  };
158
214
  this.log.info(`Processing proposal for slot ${slotNumber}`, {
159
215
  ...proposalInfo,
@@ -169,7 +225,26 @@ export class BlockProposalHandler {
169
225
  reason: 'invalid_proposal'
170
226
  };
171
227
  }
172
- // Check that the parent proposal is a block we know, otherwise reexecution would fail
228
+ // Ensure the block source is synced before checking for existing blocks,
229
+ // since a proposed checkpoint prune may remove blocks we'd otherwise find.
230
+ // This affects mostly the block_number_already_exists check, since a pending
231
+ // checkpoint prune could remove a block that would conflict with this proposal.
232
+ // When pipelining is enabled, the proposer builds ahead of L1 submission, so the
233
+ // block source won't have synced to the proposed slot yet. Skip the sync wait to
234
+ // avoid eating into the attestation window.
235
+ if (!this.epochCache.isProposerPipeliningEnabled()) {
236
+ const blockSourceSync = await this.waitForBlockSourceSync(slotNumber);
237
+ if (!blockSourceSync) {
238
+ this.log.warn(`Block source is not synced, skipping processing`, proposalInfo);
239
+ return {
240
+ isValid: false,
241
+ reason: 'block_source_not_synced'
242
+ };
243
+ }
244
+ }
245
+ // Check that the parent proposal is a block we know, otherwise reexecution would fail.
246
+ // If we don't find it immediately, we keep retrying for a while; it may be we still
247
+ // need to process other block proposals to get to it.
173
248
  const parentBlock = await this.getParentBlock(proposal);
174
249
  if (parentBlock === undefined) {
175
250
  this.log.warn(`Parent block for proposal not found, skipping processing`, proposalInfo);
@@ -192,6 +267,7 @@ export class BlockProposalHandler {
192
267
  }
193
268
  // Compute the block number based on the parent block
194
269
  const blockNumber = parentBlock === 'genesis' ? BlockNumber(INITIAL_L2_BLOCK_NUM) : BlockNumber(parentBlock.header.getBlockNumber() + 1);
270
+ proposalInfo.blockNumber = blockNumber;
195
271
  // Check that this block number does not exist already
196
272
  const existingBlock = await this.blockSource.getBlockHeader(blockNumber);
197
273
  if (existingBlock) {
@@ -208,7 +284,7 @@ export class BlockProposalHandler {
208
284
  pinnedPeer: proposalSender,
209
285
  deadline: this.getReexecutionDeadline(slotNumber, config)
210
286
  });
211
- // If reexecution is disabled, bail. We are just interested in triggering tx collection.
287
+ // If reexecution is disabled, bail. We were just interested in triggering tx collection.
212
288
  if (!shouldReexecute) {
213
289
  this.log.info(`Received valid block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`, proposalInfo);
214
290
  return {
@@ -226,6 +302,7 @@ export class BlockProposalHandler {
226
302
  };
227
303
  }
228
304
  const checkpointNumber = checkpointResult.checkpointNumber;
305
+ proposalInfo.checkpointNumber = checkpointNumber;
229
306
  // Check that I have the same set of l1ToL2Messages as the proposal
230
307
  const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
231
308
  const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
@@ -274,7 +351,7 @@ export class BlockProposalHandler {
274
351
  }
275
352
  // If we succeeded, push this block into the archiver (unless disabled)
276
353
  if (reexecutionResult?.block && this.config.skipPushProposedBlocksToArchiver === false) {
277
- await this.blockSource.addBlock(reexecutionResult?.block);
354
+ await this.blockSource.addBlock(reexecutionResult.block);
278
355
  }
279
356
  this.log.info(`Successfully re-executed block ${blockNumber} proposal at index ${proposal.indexWithinCheckpoint} on slot ${slotNumber}`, {
280
357
  ...proposalInfo,
@@ -288,13 +365,12 @@ export class BlockProposalHandler {
288
365
  }
289
366
  async getParentBlock(proposal) {
290
367
  const parentArchive = proposal.blockHeader.lastArchive.root;
291
- const slot = proposal.slotNumber;
292
368
  const config = this.checkpointsBuilder.getConfig();
293
369
  const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
294
370
  if (parentArchive.equals(genesisArchiveRoot)) {
295
371
  return 'genesis';
296
372
  }
297
- const deadline = this.getReexecutionDeadline(slot, config);
373
+ const deadline = this.getReexecutionDeadline(proposal.slotNumber, config);
298
374
  const currentTime = this.dateProvider.now();
299
375
  const timeoutDurationMs = deadline.getTime() - currentTime;
300
376
  try {
@@ -440,12 +516,47 @@ export class BlockProposalHandler {
440
516
  }
441
517
  return undefined;
442
518
  }
443
- getReexecutionDeadline(slot, config) {
444
- const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
519
+ getReexecutionDeadline(slotNumber, config) {
520
+ // Under proposer pipelining, the proposal slot may be ahead of wall clock time.
521
+ // Reexecution budgets should still be bounded by the current slot we are in now.
522
+ const wallclockSlot = slotNumber - this.epochCache.pipeliningOffset();
523
+ const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(wallclockSlot + 1), config));
445
524
  return new Date(nextSlotTimestampSeconds * 1000);
446
525
  }
526
+ /** Waits for the block source to sync L1 data up to at least the slot before the given one. */ async waitForBlockSourceSync(slot) {
527
+ const deadline = this.getReexecutionDeadline(slot, this.checkpointsBuilder.getConfig());
528
+ const timeoutMs = deadline.getTime() - this.dateProvider.now();
529
+ if (slot === 0) {
530
+ return true;
531
+ }
532
+ // Make a quick check before triggering an archiver sync
533
+ // If we are pipelining and have a pending checkpoint number stored, we will allow the block proposal to be for a slot further
534
+ const syncedSlot = await this.blockSource.getSyncedL2SlotNumber();
535
+ if (syncedSlot !== undefined && syncedSlot + 1 + this.epochCache.pipeliningOffset() >= slot) {
536
+ return true;
537
+ }
538
+ try {
539
+ // Trigger an immediate sync of the block source, and wait until it reports being synced to the required slot
540
+ return await retryUntil(async ()=>{
541
+ await this.blockSource.syncImmediate();
542
+ const updatedSyncedSlot = await this.blockSource.getSyncedL2SlotNumber();
543
+ return updatedSyncedSlot !== undefined && updatedSyncedSlot + 1 >= slot;
544
+ }, 'wait for block source sync', timeoutMs / 1000, 0.5);
545
+ } catch (err) {
546
+ if (err instanceof TimeoutError) {
547
+ this.log.warn(`Timed out waiting for block source to sync to slot ${slot}`);
548
+ return false;
549
+ } else {
550
+ throw err;
551
+ }
552
+ }
553
+ }
447
554
  getReexecuteFailureReason(err) {
448
- if (err instanceof ReExStateMismatchError) {
555
+ if (err instanceof TransactionsNotAvailableError) {
556
+ return 'txs_not_available';
557
+ } else if (err instanceof ReExInitialStateMismatchError) {
558
+ return 'initial_state_mismatch';
559
+ } else if (err instanceof ReExStateMismatchError) {
449
560
  return 'state_mismatch';
450
561
  } else if (err instanceof ReExFailedTxsError) {
451
562
  return 'failed_txs';
@@ -479,6 +590,12 @@ export class BlockProposalHandler {
479
590
  const parentBlockNumber = BlockNumber(blockNumber - 1);
480
591
  await this.worldState.syncImmediate(parentBlockNumber);
481
592
  const fork = _ts_add_disposable_resource(env, await this.worldState.fork(parentBlockNumber), true);
593
+ // Verify the fork's archive root matches the proposal's expected last archive.
594
+ // If they don't match, our world state synced to a different chain and reexecution would fail.
595
+ const forkArchiveRoot = new Fr((await fork.getTreeInfo(MerkleTreeId.ARCHIVE)).root);
596
+ if (!forkArchiveRoot.equals(proposal.blockHeader.lastArchive.root)) {
597
+ throw new ReExInitialStateMismatchError(proposal.blockHeader.lastArchive.root, forkArchiveRoot);
598
+ }
482
599
  // Build checkpoint constants from proposal (excludes blockNumber which is per-block)
483
600
  const constants = {
484
601
  chainId: new Fr(config.l1ChainId),
@@ -495,6 +612,8 @@ export class BlockProposalHandler {
495
612
  const deadline = this.getReexecutionDeadline(slot, config);
496
613
  const maxBlockGas = this.config.validateMaxL2BlockGas !== undefined || this.config.validateMaxDABlockGas !== undefined ? new Gas(this.config.validateMaxDABlockGas ?? Infinity, this.config.validateMaxL2BlockGas ?? Infinity) : undefined;
497
614
  const result = await checkpointBuilder.buildBlock(txs, blockNumber, blockHeader.globalVariables.timestamp, {
615
+ isBuildingProposal: false,
616
+ minValidTxs: 0,
498
617
  deadline,
499
618
  expectedEndState: blockHeader.state,
500
619
  maxTransactions: this.config.validateMaxTxsPerBlock,
@@ -548,4 +667,308 @@ export class BlockProposalHandler {
548
667
  if (result) await result;
549
668
  }
550
669
  }
670
+ /**
671
+ * Validates a checkpoint proposal, caches the result, and uploads blobs if configured.
672
+ * Returns a cached result if the same proposal (archive + slot) was already validated.
673
+ * Used by both the all-nodes callback (via register) and the validator client (via delegation).
674
+ */ async handleCheckpointProposal(proposal, proposalInfo) {
675
+ const slot = proposal.slotNumber;
676
+ // Check cache: same archive+slot means we already validated this proposal
677
+ if (this.lastCheckpointValidationResult && this.lastCheckpointValidationResult.archive.equals(proposal.archive) && this.lastCheckpointValidationResult.slotNumber === slot) {
678
+ this.log.debug(`Returning cached validation result for checkpoint proposal at slot ${slot}`, proposalInfo);
679
+ return this.lastCheckpointValidationResult.result;
680
+ }
681
+ const proposer = proposal.getSender();
682
+ if (!proposer) {
683
+ this.log.warn(`Received checkpoint proposal with invalid signature for slot ${proposal.slotNumber}`);
684
+ const result = {
685
+ isValid: false,
686
+ reason: 'invalid_signature'
687
+ };
688
+ this.lastCheckpointValidationResult = {
689
+ archive: proposal.archive,
690
+ slotNumber: slot,
691
+ result
692
+ };
693
+ return result;
694
+ }
695
+ if (!validateFeeAssetPriceModifier(proposal.feeAssetPriceModifier)) {
696
+ this.log.warn(`Received checkpoint proposal with invalid feeAssetPriceModifier ${proposal.feeAssetPriceModifier} for slot ${proposal.slotNumber}`);
697
+ const result = {
698
+ isValid: false,
699
+ reason: 'invalid_fee_asset_price_modifier'
700
+ };
701
+ this.lastCheckpointValidationResult = {
702
+ archive: proposal.archive,
703
+ slotNumber: slot,
704
+ result
705
+ };
706
+ return result;
707
+ }
708
+ const result = await this.validateCheckpointProposal(proposal, proposalInfo);
709
+ this.lastCheckpointValidationResult = {
710
+ archive: proposal.archive,
711
+ slotNumber: slot,
712
+ result
713
+ };
714
+ // Upload blobs to filestore if validation passed (fire and forget)
715
+ if (result.isValid) {
716
+ this.tryUploadBlobsForCheckpoint(proposal, proposalInfo);
717
+ }
718
+ return result;
719
+ }
720
+ /**
721
+ * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
722
+ * @returns Validation result with isValid flag and reason if invalid.
723
+ */ async validateCheckpointProposal(proposal, proposalInfo) {
724
+ const env = {
725
+ stack: [],
726
+ error: void 0,
727
+ hasError: false
728
+ };
729
+ try {
730
+ const slot = proposal.slotNumber;
731
+ // Timeout block syncing at the start of the next slot
732
+ const config = this.checkpointsBuilder.getConfig();
733
+ const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
734
+ const timeoutSeconds = Math.max(1, nextSlotTimestampSeconds - Math.floor(this.dateProvider.now() / 1000));
735
+ // Wait for last block to sync by archive
736
+ let lastBlockHeader;
737
+ try {
738
+ lastBlockHeader = await retryUntil(async ()=>{
739
+ await this.blockSource.syncImmediate();
740
+ return this.blockSource.getBlockHeaderByArchive(proposal.archive);
741
+ }, `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`, timeoutSeconds, 0.5);
742
+ } catch (err) {
743
+ if (err instanceof TimeoutError) {
744
+ this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
745
+ return {
746
+ isValid: false,
747
+ reason: 'last_block_not_found'
748
+ };
749
+ }
750
+ this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
751
+ return {
752
+ isValid: false,
753
+ reason: 'block_fetch_error'
754
+ };
755
+ }
756
+ if (!lastBlockHeader) {
757
+ this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
758
+ return {
759
+ isValid: false,
760
+ reason: 'last_block_not_found'
761
+ };
762
+ }
763
+ // Get all full blocks for the slot and checkpoint
764
+ const blocks = await this.blockSource.getBlocksForSlot(slot);
765
+ if (blocks.length === 0) {
766
+ this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
767
+ return {
768
+ isValid: false,
769
+ reason: 'no_blocks_for_slot'
770
+ };
771
+ }
772
+ // Ensure the last block for this slot matches the archive in the checkpoint proposal
773
+ if (!blocks.at(-1)?.archive.root.equals(proposal.archive)) {
774
+ this.log.warn(`Last block archive mismatch for checkpoint proposal`, proposalInfo);
775
+ return {
776
+ isValid: false,
777
+ reason: 'last_block_archive_mismatch'
778
+ };
779
+ }
780
+ this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
781
+ ...proposalInfo,
782
+ blockNumbers: blocks.map((b)=>b.number)
783
+ });
784
+ // Get checkpoint constants from first block
785
+ const firstBlock = blocks[0];
786
+ const constants = this.extractCheckpointConstants(firstBlock);
787
+ const checkpointNumber = firstBlock.checkpointNumber;
788
+ // Get L1-to-L2 messages for this checkpoint
789
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
790
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
791
+ const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
792
+ const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch)).filter((c)=>c.checkpointNumber < checkpointNumber).map((c)=>c.checkpointOutHash);
793
+ // Fork world state at the block before the first block
794
+ const parentBlockNumber = BlockNumber(firstBlock.number - 1);
795
+ const fork = _ts_add_disposable_resource(env, await this.checkpointsBuilder.getFork(parentBlockNumber), true);
796
+ // Create checkpoint builder with all existing blocks
797
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(checkpointNumber, constants, proposal.feeAssetPriceModifier, l1ToL2Messages, previousCheckpointOutHashes, fork, blocks, this.log.getBindings());
798
+ // Complete the checkpoint to get computed values
799
+ const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
800
+ // Compare checkpoint header with proposal
801
+ if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
802
+ this.log.warn(`Checkpoint header mismatch`, {
803
+ ...proposalInfo,
804
+ computed: computedCheckpoint.header.toInspect(),
805
+ proposal: proposal.checkpointHeader.toInspect()
806
+ });
807
+ return {
808
+ isValid: false,
809
+ reason: 'checkpoint_header_mismatch'
810
+ };
811
+ }
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 {
820
+ isValid: false,
821
+ reason: 'archive_mismatch'
822
+ };
823
+ }
824
+ // Check that the accumulated epoch out hash matches the value in the proposal.
825
+ // The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
826
+ const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
827
+ const computedEpochOutHash = accumulateCheckpointOutHashes([
828
+ ...previousCheckpointOutHashes,
829
+ checkpointOutHash
830
+ ]);
831
+ const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
832
+ if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
833
+ this.log.warn(`Epoch out hash mismatch`, {
834
+ proposalEpochOutHash: proposalEpochOutHash.toString(),
835
+ computedEpochOutHash: computedEpochOutHash.toString(),
836
+ checkpointOutHash: checkpointOutHash.toString(),
837
+ previousCheckpointOutHashes: previousCheckpointOutHashes.map((h)=>h.toString()),
838
+ ...proposalInfo
839
+ });
840
+ return {
841
+ isValid: false,
842
+ reason: 'out_hash_mismatch'
843
+ };
844
+ }
845
+ // Final round of validations on the checkpoint, just in case.
846
+ try {
847
+ validateCheckpoint(computedCheckpoint, {
848
+ rollupManaLimit: this.checkpointsBuilder.getConfig().rollupManaLimit,
849
+ maxDABlockGas: this.config.validateMaxDABlockGas,
850
+ maxL2BlockGas: this.config.validateMaxL2BlockGas,
851
+ maxTxsPerBlock: this.config.validateMaxTxsPerBlock,
852
+ maxTxsPerCheckpoint: this.config.validateMaxTxsPerCheckpoint
853
+ });
854
+ } catch (err) {
855
+ this.log.warn(`Checkpoint validation failed: ${err}`, proposalInfo);
856
+ return {
857
+ isValid: false,
858
+ reason: 'checkpoint_validation_failed'
859
+ };
860
+ }
861
+ this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
862
+ return {
863
+ isValid: true,
864
+ checkpointNumber
865
+ };
866
+ } catch (e) {
867
+ env.error = e;
868
+ env.hasError = true;
869
+ } finally{
870
+ const result = _ts_dispose_resources(env);
871
+ if (result) await result;
872
+ }
873
+ }
874
+ /** Extracts checkpoint global variables from a block. */ extractCheckpointConstants(block) {
875
+ const gv = block.header.globalVariables;
876
+ return {
877
+ chainId: gv.chainId,
878
+ version: gv.version,
879
+ slotNumber: gv.slotNumber,
880
+ timestamp: gv.timestamp,
881
+ coinbase: gv.coinbase,
882
+ feeRecipient: gv.feeRecipient,
883
+ gasFees: gv.gasFees
884
+ };
885
+ }
886
+ /** Triggers blob upload for a checkpoint if the blob client can upload (fire and forget). */ tryUploadBlobsForCheckpoint(proposal, proposalInfo) {
887
+ if (this.blobClient.canUpload()) {
888
+ void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
889
+ }
890
+ }
891
+ /** Uploads blobs for a checkpoint to the filestore. */ async uploadBlobsForCheckpoint(proposal, proposalInfo) {
892
+ try {
893
+ const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
894
+ if (!lastBlockHeader) {
895
+ this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
896
+ return;
897
+ }
898
+ const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
899
+ if (blocks.length === 0) {
900
+ this.log.warn(`No blocks found for blob upload`, proposalInfo);
901
+ return;
902
+ }
903
+ const blockBlobData = blocks.map((b)=>b.toBlockBlobData());
904
+ const blobFields = encodeCheckpointBlobDataFromBlocks(blockBlobData);
905
+ const blobs = await getBlobsPerL1Block(blobFields);
906
+ await this.blobClient.sendBlobsToFilestore(blobs);
907
+ this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
908
+ ...proposalInfo,
909
+ numBlobs: blobs.length
910
+ });
911
+ } catch (err) {
912
+ this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
913
+ }
914
+ }
915
+ /**
916
+ * Derives proposed checkpoint data from validated blocks and sets it on the archiver.
917
+ * Used after successful validation of a foreign proposal.
918
+ * Does not retry since we already waited for the block during validation.
919
+ */ async setProposedCheckpointFromValidation(proposal) {
920
+ if (!this.archiver) {
921
+ return false;
922
+ }
923
+ const blockData = await this.blockSource.getBlockDataByArchive(proposal.archive);
924
+ if (!blockData) {
925
+ this.log.debug(`Block data not found for checkpoint proposal archive, cannot set proposed checkpoint`, {
926
+ archive: proposal.archive.toString()
927
+ });
928
+ return false;
929
+ }
930
+ await this.archiver.setProposedCheckpoint({
931
+ header: proposal.checkpointHeader,
932
+ checkpointNumber: blockData.checkpointNumber,
933
+ startBlock: BlockNumber(blockData.header.getBlockNumber() - blockData.indexWithinCheckpoint),
934
+ blockCount: blockData.indexWithinCheckpoint + 1,
935
+ totalManaUsed: proposal.checkpointHeader.totalManaUsed.toBigInt(),
936
+ feeAssetPriceModifier: proposal.feeAssetPriceModifier
937
+ });
938
+ return true;
939
+ }
940
+ /**
941
+ * Sets proposed checkpoint from blocks for own proposals (skips full validation).
942
+ * Retries fetching block data since the checkpoint proposal often arrives before the last block
943
+ * finishes re-execution.
944
+ */ async setProposedCheckpointFromBlocks(proposal) {
945
+ if (!this.archiver) {
946
+ return false;
947
+ }
948
+ let blockData = await this.blockSource.getBlockDataByArchive(proposal.archive);
949
+ if (!blockData) {
950
+ // The checkpoint proposal often arrives before the last block finishes re-execution.
951
+ // Retry until we find the data or give up at the end of the slot.
952
+ const nextSlot = this.epochCache.getSlotNow() + 1;
953
+ const timeOfNextSlot = getTimestampForSlot(SlotNumber(nextSlot), await this.archiver.getL1Constants());
954
+ const timeoutSeconds = Math.max(1, Number(timeOfNextSlot) - Math.floor(this.dateProvider.now() / 1000));
955
+ blockData = await retryUntil(()=>this.blockSource.getBlockDataByArchive(proposal.archive), 'block data for own checkpoint proposal', timeoutSeconds, 0.25).catch(()=>undefined);
956
+ }
957
+ if (blockData) {
958
+ await this.archiver.setProposedCheckpoint({
959
+ header: proposal.checkpointHeader,
960
+ checkpointNumber: blockData.checkpointNumber,
961
+ startBlock: BlockNumber(blockData.header.getBlockNumber() - blockData.indexWithinCheckpoint),
962
+ blockCount: blockData.indexWithinCheckpoint + 1,
963
+ totalManaUsed: proposal.checkpointHeader.totalManaUsed.toBigInt(),
964
+ feeAssetPriceModifier: proposal.feeAssetPriceModifier
965
+ });
966
+ return true;
967
+ } else {
968
+ this.log.debug(`Block data not found for own checkpoint proposal archive, cannot set proposed checkpoint`, {
969
+ archive: proposal.archive.toString()
970
+ });
971
+ return false;
972
+ }
973
+ }
551
974
  }