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

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,20 +1,29 @@
1
+ import type { BlobClientInterface } from '@aztec/blob-client/client';
2
+ import { type Blob, encodeCheckpointBlobDataFromBlocks, getBlobsPerL1Block } from '@aztec/blob-lib';
1
3
  import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
4
  import type { EpochCache } from '@aztec/epoch-cache';
5
+ import { validateFeeAssetPriceModifier } from '@aztec/ethereum/contracts';
3
6
  import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
4
7
  import { pick } from '@aztec/foundation/collection';
5
8
  import { Fr } from '@aztec/foundation/curves/bn254';
6
9
  import { TimeoutError } from '@aztec/foundation/error';
10
+ import type { LogData } from '@aztec/foundation/log';
7
11
  import { createLogger } from '@aztec/foundation/log';
8
12
  import { retryUntil } from '@aztec/foundation/retry';
9
13
  import { DateProvider, Timer } from '@aztec/foundation/timer';
10
14
  import type { P2P, PeerId } from '@aztec/p2p';
11
15
  import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
12
16
  import type { BlockData, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
17
+ import { validateCheckpoint } from '@aztec/stdlib/checkpoint';
13
18
  import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
14
19
  import { Gas } from '@aztec/stdlib/gas';
15
20
  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';
21
+ import {
22
+ type L1ToL2MessageSource,
23
+ accumulateCheckpointOutHashes,
24
+ computeInHashFromL1ToL2Messages,
25
+ } from '@aztec/stdlib/messaging';
26
+ import type { BlockProposal, CheckpointProposalCore } from '@aztec/stdlib/p2p';
18
27
  import { MerkleTreeId } from '@aztec/stdlib/trees';
19
28
  import type { CheckpointGlobalVariables, FailedTx, Tx } from '@aztec/stdlib/tx';
20
29
  import {
@@ -66,11 +75,14 @@ export type BlockProposalValidationFailureResult = {
66
75
 
67
76
  export type BlockProposalValidationResult = BlockProposalValidationSuccessResult | BlockProposalValidationFailureResult;
68
77
 
78
+ export type CheckpointProposalValidationResult = { isValid: true } | { isValid: false; reason: string };
79
+
69
80
  type CheckpointComputationResult =
70
81
  | { checkpointNumber: CheckpointNumber; reason?: undefined }
71
82
  | { checkpointNumber?: undefined; reason: 'invalid_proposal' | 'global_variables_mismatch' };
72
83
 
73
- export class BlockProposalHandler {
84
+ /** Handles block and checkpoint proposals for both validator and non-validator nodes. */
85
+ export class ProposalHandler {
74
86
  public readonly tracer: Tracer;
75
87
 
76
88
  constructor(
@@ -82,21 +94,26 @@ export class BlockProposalHandler {
82
94
  private blockProposalValidator: BlockProposalValidator,
83
95
  private epochCache: EpochCache,
84
96
  private config: ValidatorClientFullConfig,
97
+ private blobClient: BlobClientInterface,
85
98
  private metrics?: ValidatorMetrics,
86
99
  private dateProvider: DateProvider = new DateProvider(),
87
100
  telemetry: TelemetryClient = getTelemetryClient(),
88
- private log = createLogger('validator:block-proposal-handler'),
101
+ private log = createLogger('validator:proposal-handler'),
89
102
  ) {
90
103
  if (config.fishermanMode) {
91
104
  this.log = this.log.createChild('[FISHERMAN]');
92
105
  }
93
- this.tracer = telemetry.getTracer('BlockProposalHandler');
106
+ this.tracer = telemetry.getTracer('ProposalHandler');
94
107
  }
95
108
 
96
- register(p2pClient: P2P, shouldReexecute: boolean): BlockProposalHandler {
109
+ /**
110
+ * Registers non-validator handlers for block and checkpoint proposals on the p2p client.
111
+ * Block proposals are always registered. Checkpoint proposals are registered if the blob client can upload.
112
+ */
113
+ register(p2pClient: P2P, shouldReexecute: boolean): ProposalHandler {
97
114
  // Non-validator handler that processes or re-executes for monitoring but does not attest.
98
115
  // Returns boolean indicating whether the proposal was valid.
99
- const handler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
116
+ const blockHandler = async (proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> => {
100
117
  try {
101
118
  const { slotNumber, blockNumber } = proposal;
102
119
  const result = await this.handleBlockProposal(proposal, proposalSender, shouldReexecute);
@@ -123,7 +140,35 @@ export class BlockProposalHandler {
123
140
  }
124
141
  };
125
142
 
126
- p2pClient.registerBlockProposalHandler(handler);
143
+ p2pClient.registerBlockProposalHandler(blockHandler);
144
+
145
+ // Register checkpoint proposal handler if blob uploads are enabled and we are reexecuting
146
+ if (this.blobClient.canUpload() && shouldReexecute) {
147
+ const checkpointHandler = async (checkpoint: CheckpointProposalCore, _sender: PeerId) => {
148
+ try {
149
+ const proposalInfo = {
150
+ proposalSlotNumber: checkpoint.slotNumber,
151
+ archive: checkpoint.archive.toString(),
152
+ proposer: checkpoint.getSender()?.toString(),
153
+ };
154
+ const result = await this.handleCheckpointProposal(checkpoint, proposalInfo);
155
+ if (result.isValid) {
156
+ this.log.info(`Non-validator checkpoint proposal at slot ${checkpoint.slotNumber} handled`, proposalInfo);
157
+ } else {
158
+ this.log.warn(
159
+ `Non-validator checkpoint proposal at slot ${checkpoint.slotNumber} failed: ${result.reason}`,
160
+ proposalInfo,
161
+ );
162
+ }
163
+ } catch (error) {
164
+ this.log.error('Error processing checkpoint proposal in non-validator handler', error);
165
+ }
166
+ // Non-validators don't attest
167
+ return undefined;
168
+ };
169
+ p2pClient.registerCheckpointProposalHandler(checkpointHandler);
170
+ }
171
+
127
172
  return this;
128
173
  }
129
174
 
@@ -629,4 +674,234 @@ export class BlockProposalHandler {
629
674
  totalManaUsed,
630
675
  };
631
676
  }
677
+
678
+ /**
679
+ * Validates a checkpoint proposal and uploads blobs if configured.
680
+ * Used by both non-validator nodes (via register) and the validator client (via delegation).
681
+ */
682
+ async handleCheckpointProposal(
683
+ proposal: CheckpointProposalCore,
684
+ proposalInfo: LogData,
685
+ ): Promise<CheckpointProposalValidationResult> {
686
+ const proposer = proposal.getSender();
687
+ if (!proposer) {
688
+ this.log.warn(`Received checkpoint proposal with invalid signature for slot ${proposal.slotNumber}`);
689
+ return { isValid: false, reason: 'invalid_signature' };
690
+ }
691
+
692
+ if (!validateFeeAssetPriceModifier(proposal.feeAssetPriceModifier)) {
693
+ this.log.warn(
694
+ `Received checkpoint proposal with invalid feeAssetPriceModifier ${proposal.feeAssetPriceModifier} for slot ${proposal.slotNumber}`,
695
+ );
696
+ return { isValid: false, reason: 'invalid_fee_asset_price_modifier' };
697
+ }
698
+
699
+ const result = await this.validateCheckpointProposal(proposal, proposalInfo);
700
+
701
+ // Upload blobs to filestore if validation passed (fire and forget)
702
+ if (result.isValid) {
703
+ this.tryUploadBlobsForCheckpoint(proposal, proposalInfo);
704
+ }
705
+
706
+ return result;
707
+ }
708
+
709
+ /**
710
+ * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
711
+ * @returns Validation result with isValid flag and reason if invalid.
712
+ */
713
+ async validateCheckpointProposal(
714
+ proposal: CheckpointProposalCore,
715
+ proposalInfo: LogData,
716
+ ): Promise<CheckpointProposalValidationResult> {
717
+ const slot = proposal.slotNumber;
718
+
719
+ // Timeout block syncing at the start of the next slot
720
+ const config = this.checkpointsBuilder.getConfig();
721
+ const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
722
+ const timeoutSeconds = Math.max(1, nextSlotTimestampSeconds - Math.floor(this.dateProvider.now() / 1000));
723
+
724
+ // Wait for last block to sync by archive
725
+ let lastBlockHeader;
726
+ try {
727
+ lastBlockHeader = await retryUntil(
728
+ async () => {
729
+ await this.blockSource.syncImmediate();
730
+ return this.blockSource.getBlockHeaderByArchive(proposal.archive);
731
+ },
732
+ `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`,
733
+ timeoutSeconds,
734
+ 0.5,
735
+ );
736
+ } catch (err) {
737
+ if (err instanceof TimeoutError) {
738
+ this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
739
+ return { isValid: false, reason: 'last_block_not_found' };
740
+ }
741
+ this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
742
+ return { isValid: false, reason: 'block_fetch_error' };
743
+ }
744
+
745
+ if (!lastBlockHeader) {
746
+ this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
747
+ return { isValid: false, reason: 'last_block_not_found' };
748
+ }
749
+
750
+ // Get all full blocks for the slot and checkpoint
751
+ const blocks = await this.blockSource.getBlocksForSlot(slot);
752
+ if (blocks.length === 0) {
753
+ this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
754
+ return { isValid: false, reason: 'no_blocks_for_slot' };
755
+ }
756
+
757
+ // Ensure the last block for this slot matches the archive in the checkpoint proposal
758
+ if (!blocks.at(-1)?.archive.root.equals(proposal.archive)) {
759
+ this.log.warn(`Last block archive mismatch for checkpoint proposal`, proposalInfo);
760
+ return { isValid: false, reason: 'last_block_archive_mismatch' };
761
+ }
762
+
763
+ this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
764
+ ...proposalInfo,
765
+ blockNumbers: blocks.map(b => b.number),
766
+ });
767
+
768
+ // Get checkpoint constants from first block
769
+ const firstBlock = blocks[0];
770
+ const constants = this.extractCheckpointConstants(firstBlock);
771
+ const checkpointNumber = firstBlock.checkpointNumber;
772
+
773
+ // Get L1-to-L2 messages for this checkpoint
774
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
775
+
776
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
777
+ const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
778
+ const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
779
+ .filter(c => c.checkpointNumber < checkpointNumber)
780
+ .map(c => c.checkpointOutHash);
781
+
782
+ // Fork world state at the block before the first block
783
+ const parentBlockNumber = BlockNumber(firstBlock.number - 1);
784
+ const fork = await this.worldState.fork(parentBlockNumber);
785
+
786
+ try {
787
+ // Create checkpoint builder with all existing blocks
788
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
789
+ checkpointNumber,
790
+ constants,
791
+ proposal.feeAssetPriceModifier,
792
+ l1ToL2Messages,
793
+ previousCheckpointOutHashes,
794
+ fork,
795
+ blocks,
796
+ this.log.getBindings(),
797
+ );
798
+
799
+ // Complete the checkpoint to get computed values
800
+ const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
801
+
802
+ // Compare checkpoint header with proposal
803
+ if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
804
+ this.log.warn(`Checkpoint header mismatch`, {
805
+ ...proposalInfo,
806
+ computed: computedCheckpoint.header.toInspect(),
807
+ proposal: proposal.checkpointHeader.toInspect(),
808
+ });
809
+ return { isValid: false, reason: 'checkpoint_header_mismatch' };
810
+ }
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 { isValid: false, reason: 'archive_mismatch' };
820
+ }
821
+
822
+ // Check that the accumulated epoch out hash matches the value in the proposal.
823
+ // The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
824
+ const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
825
+ const computedEpochOutHash = accumulateCheckpointOutHashes([...previousCheckpointOutHashes, checkpointOutHash]);
826
+ const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
827
+ if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
828
+ this.log.warn(`Epoch out hash mismatch`, {
829
+ proposalEpochOutHash: proposalEpochOutHash.toString(),
830
+ computedEpochOutHash: computedEpochOutHash.toString(),
831
+ checkpointOutHash: checkpointOutHash.toString(),
832
+ previousCheckpointOutHashes: previousCheckpointOutHashes.map(h => h.toString()),
833
+ ...proposalInfo,
834
+ });
835
+ return { isValid: false, reason: 'out_hash_mismatch' };
836
+ }
837
+
838
+ // Final round of validations on the checkpoint, just in case.
839
+ try {
840
+ validateCheckpoint(computedCheckpoint, {
841
+ rollupManaLimit: this.checkpointsBuilder.getConfig().rollupManaLimit,
842
+ maxDABlockGas: this.config.validateMaxDABlockGas,
843
+ maxL2BlockGas: this.config.validateMaxL2BlockGas,
844
+ maxTxsPerBlock: this.config.validateMaxTxsPerBlock,
845
+ maxTxsPerCheckpoint: this.config.validateMaxTxsPerCheckpoint,
846
+ });
847
+ } catch (err) {
848
+ this.log.warn(`Checkpoint validation failed: ${err}`, proposalInfo);
849
+ return { isValid: false, reason: 'checkpoint_validation_failed' };
850
+ }
851
+
852
+ this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
853
+ return { isValid: true };
854
+ } finally {
855
+ await fork.close();
856
+ }
857
+ }
858
+
859
+ /** Extracts checkpoint global variables from a block. */
860
+ private extractCheckpointConstants(block: L2Block): CheckpointGlobalVariables {
861
+ const gv = block.header.globalVariables;
862
+ return {
863
+ chainId: gv.chainId,
864
+ version: gv.version,
865
+ slotNumber: gv.slotNumber,
866
+ timestamp: gv.timestamp,
867
+ coinbase: gv.coinbase,
868
+ feeRecipient: gv.feeRecipient,
869
+ gasFees: gv.gasFees,
870
+ };
871
+ }
872
+
873
+ /** Triggers blob upload for a checkpoint if the blob client can upload (fire and forget). */
874
+ protected tryUploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): void {
875
+ if (this.blobClient.canUpload()) {
876
+ void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
877
+ }
878
+ }
879
+
880
+ /** Uploads blobs for a checkpoint to the filestore. */
881
+ protected async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
882
+ try {
883
+ const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
884
+ if (!lastBlockHeader) {
885
+ this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
886
+ return;
887
+ }
888
+
889
+ const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
890
+ if (blocks.length === 0) {
891
+ this.log.warn(`No blocks found for blob upload`, proposalInfo);
892
+ return;
893
+ }
894
+
895
+ const blockBlobData = blocks.map(b => b.toBlockBlobData());
896
+ const blobFields = encodeCheckpointBlobDataFromBlocks(blockBlobData);
897
+ const blobs: Blob[] = await getBlobsPerL1Block(blobFields);
898
+ await this.blobClient.sendBlobsToFilestore(blobs);
899
+ this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
900
+ ...proposalInfo,
901
+ numBlobs: blobs.length,
902
+ });
903
+ } catch (err) {
904
+ this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
905
+ }
906
+ }
632
907
  }