@aztec/validator-client 0.0.1-commit.64b6bbb → 0.0.1-commit.69c59a8b3

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 (52) hide show
  1. package/README.md +41 -2
  2. package/dest/checkpoint_builder.d.ts +21 -8
  3. package/dest/checkpoint_builder.d.ts.map +1 -1
  4. package/dest/checkpoint_builder.js +124 -46
  5. package/dest/config.d.ts +1 -1
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +22 -6
  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 +15 -33
  11. package/dest/factory.d.ts +7 -4
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +6 -5
  14. package/dest/index.d.ts +2 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +1 -2
  17. package/dest/key_store/ha_key_store.js +1 -1
  18. package/dest/metrics.d.ts +14 -2
  19. package/dest/metrics.d.ts.map +1 -1
  20. package/dest/metrics.js +24 -0
  21. package/dest/proposal_handler.d.ts +108 -0
  22. package/dest/proposal_handler.d.ts.map +1 -0
  23. package/dest/proposal_handler.js +974 -0
  24. package/dest/validator.d.ts +23 -20
  25. package/dest/validator.d.ts.map +1 -1
  26. package/dest/validator.js +95 -202
  27. package/package.json +19 -19
  28. package/src/checkpoint_builder.ts +142 -39
  29. package/src/config.ts +22 -6
  30. package/src/duties/validation_service.ts +25 -37
  31. package/src/factory.ts +10 -3
  32. package/src/index.ts +1 -2
  33. package/src/key_store/ha_key_store.ts +1 -1
  34. package/src/metrics.ts +37 -1
  35. package/src/proposal_handler.ts +1042 -0
  36. package/src/validator.ts +129 -224
  37. package/dest/block_proposal_handler.d.ts +0 -63
  38. package/dest/block_proposal_handler.d.ts.map +0 -1
  39. package/dest/block_proposal_handler.js +0 -546
  40. package/dest/tx_validator/index.d.ts +0 -3
  41. package/dest/tx_validator/index.d.ts.map +0 -1
  42. package/dest/tx_validator/index.js +0 -2
  43. package/dest/tx_validator/nullifier_cache.d.ts +0 -14
  44. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  45. package/dest/tx_validator/nullifier_cache.js +0 -24
  46. package/dest/tx_validator/tx_validator_factory.d.ts +0 -19
  47. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  48. package/dest/tx_validator/tx_validator_factory.js +0 -54
  49. package/src/block_proposal_handler.ts +0 -555
  50. package/src/tx_validator/index.ts +0 -2
  51. package/src/tx_validator/nullifier_cache.ts +0 -30
  52. package/src/tx_validator/tx_validator_factory.ts +0 -154
package/src/validator.ts CHANGED
@@ -1,19 +1,11 @@
1
1
  import type { BlobClientInterface } from '@aztec/blob-client/client';
2
2
  import { type Blob, getBlobsPerL1Block } from '@aztec/blob-lib';
3
3
  import type { EpochCache } from '@aztec/epoch-cache';
4
- import {
5
- BlockNumber,
6
- CheckpointNumber,
7
- EpochNumber,
8
- IndexWithinCheckpoint,
9
- SlotNumber,
10
- } from '@aztec/foundation/branded-types';
4
+ import { CheckpointNumber, EpochNumber, IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
11
5
  import { Fr } from '@aztec/foundation/curves/bn254';
12
- import { TimeoutError } from '@aztec/foundation/error';
13
6
  import type { EthAddress } from '@aztec/foundation/eth-address';
14
7
  import type { Signature } from '@aztec/foundation/eth-signature';
15
8
  import { type LogData, type Logger, createLogger } from '@aztec/foundation/log';
16
- import { retryUntil } from '@aztec/foundation/retry';
17
9
  import { RunningPromise } from '@aztec/foundation/running-promise';
18
10
  import { sleep } from '@aztec/foundation/sleep';
19
11
  import { DateProvider } from '@aztec/foundation/timer';
@@ -22,16 +14,15 @@ import type { DuplicateAttestationInfo, DuplicateProposalInfo, P2P, PeerId } fro
22
14
  import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
23
15
  import { OffenseType, WANT_TO_SLASH_EVENT, type Watcher, type WatcherEmitter } from '@aztec/slasher';
24
16
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
25
- import type { CommitteeAttestationsAndSigners, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
17
+ import type { CommitteeAttestationsAndSigners, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
26
18
  import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
27
19
  import type {
28
- CreateCheckpointProposalLastBlockData,
29
20
  ITxProvider,
30
21
  Validator,
31
22
  ValidatorClientFullConfig,
32
23
  WorldStateSynchronizer,
33
24
  } from '@aztec/stdlib/interfaces/server';
34
- import { type L1ToL2MessageSource, accumulateCheckpointOutHashes } from '@aztec/stdlib/messaging';
25
+ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
35
26
  import {
36
27
  type BlockProposal,
37
28
  type BlockProposalOptions,
@@ -41,22 +32,27 @@ import {
41
32
  type CheckpointProposalOptions,
42
33
  } from '@aztec/stdlib/p2p';
43
34
  import type { CheckpointHeader } from '@aztec/stdlib/rollup';
44
- import type { BlockHeader, CheckpointGlobalVariables, Tx } from '@aztec/stdlib/tx';
35
+ import type { BlockHeader, Tx } from '@aztec/stdlib/tx';
45
36
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
46
37
  import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
47
- import { createHASigner } from '@aztec/validator-ha-signer/factory';
48
- import { DutyType, type SigningContext } from '@aztec/validator-ha-signer/types';
38
+ import {
39
+ createHASigner,
40
+ createLocalSignerWithProtection,
41
+ createSignerFromSharedDb,
42
+ } from '@aztec/validator-ha-signer/factory';
43
+ import { DutyType, type SigningContext, type SlashingProtectionDatabase } from '@aztec/validator-ha-signer/types';
44
+ import type { ValidatorHASigner } from '@aztec/validator-ha-signer/validator-ha-signer';
49
45
 
50
46
  import { EventEmitter } from 'events';
51
47
  import type { TypedDataDefinition } from 'viem';
52
48
 
53
- import { BlockProposalHandler, type BlockProposalValidationFailureReason } from './block_proposal_handler.js';
54
49
  import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
55
50
  import { ValidationService } from './duties/validation_service.js';
56
51
  import { HAKeyStore } from './key_store/ha_key_store.js';
57
52
  import type { ExtendedValidatorKeyStore } from './key_store/interface.js';
58
53
  import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
59
54
  import { ValidatorMetrics } from './metrics.js';
55
+ import { type BlockProposalValidationFailureReason, ProposalHandler } from './proposal_handler.js';
60
56
 
61
57
  // We maintain a set of proposers who have proposed invalid blocks.
62
58
  // Just cap the set to avoid unbounded growth.
@@ -76,7 +72,6 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
76
72
  private validationService: ValidationService;
77
73
  private metrics: ValidatorMetrics;
78
74
  private log: Logger;
79
-
80
75
  // Whether it has already registered handlers on the p2p client
81
76
  private hasRegisteredHandlers = false;
82
77
 
@@ -88,6 +83,8 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
88
83
 
89
84
  private lastEpochForCommitteeUpdateLoop: EpochNumber | undefined;
90
85
  private epochCacheUpdateLoop: RunningPromise;
86
+ /** Tracks the last epoch in which each attester successfully submitted at least one attestation. */
87
+ private lastAttestedEpochByAttester: Map<string, EpochNumber> = new Map();
91
88
 
92
89
  private proposersOfInvalidBlocks: Set<string> = new Set();
93
90
 
@@ -98,13 +95,14 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
98
95
  private keyStore: ExtendedValidatorKeyStore,
99
96
  private epochCache: EpochCache,
100
97
  private p2pClient: P2P,
101
- private blockProposalHandler: BlockProposalHandler,
98
+ private proposalHandler: ProposalHandler,
102
99
  private blockSource: L2BlockSource,
103
100
  private checkpointsBuilder: FullNodeCheckpointsBuilder,
104
101
  private worldState: WorldStateSynchronizer,
105
102
  private l1ToL2MessageSource: L1ToL2MessageSource,
106
103
  private config: ValidatorClientFullConfig,
107
104
  private blobClient: BlobClientInterface,
105
+ private slashingProtectionSigner: ValidatorHASigner,
108
106
  private dateProvider: DateProvider = new DateProvider(),
109
107
  telemetry: TelemetryClient = getTelemetryClient(),
110
108
  log = createLogger('validator'),
@@ -158,6 +156,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
158
156
  this.log.trace(`No committee found for slot`);
159
157
  return;
160
158
  }
159
+ this.metrics.setCurrentEpoch(epoch);
161
160
  if (epoch !== this.lastEpochForCommitteeUpdateLoop) {
162
161
  const me = this.getValidatorAddresses();
163
162
  const committeeSet = new Set(committee.map(v => v.toString()));
@@ -191,12 +190,14 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
191
190
  blobClient: BlobClientInterface,
192
191
  dateProvider: DateProvider = new DateProvider(),
193
192
  telemetry: TelemetryClient = getTelemetryClient(),
193
+ slashingProtectionDb?: SlashingProtectionDatabase,
194
194
  ) {
195
195
  const metrics = new ValidatorMetrics(telemetry);
196
196
  const blockProposalValidator = new BlockProposalValidator(epochCache, {
197
197
  txsPermitted: !config.disableTransactions,
198
+ maxTxsPerBlock: config.validateMaxTxsPerBlock,
198
199
  });
199
- const blockProposalHandler = new BlockProposalHandler(
200
+ const proposalHandler = new ProposalHandler(
200
201
  checkpointsBuilder,
201
202
  worldState,
202
203
  blockSource,
@@ -205,33 +206,54 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
205
206
  blockProposalValidator,
206
207
  epochCache,
207
208
  config,
209
+ blobClient,
208
210
  metrics,
209
211
  dateProvider,
210
212
  telemetry,
213
+ undefined,
211
214
  );
212
215
 
213
- let validatorKeyStore: ExtendedValidatorKeyStore = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
214
- if (config.haSigningEnabled) {
216
+ const nodeKeystoreAdapter = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
217
+ let slashingProtectionSigner: ValidatorHASigner;
218
+ if (slashingProtectionDb) {
219
+ // Shared database mode: use a pre-existing database (e.g. for testing HA setups).
220
+ ({ signer: slashingProtectionSigner } = createSignerFromSharedDb(slashingProtectionDb, config, {
221
+ telemetryClient: telemetry,
222
+ dateProvider,
223
+ }));
224
+ } else if (config.haSigningEnabled) {
225
+ // Multi-node HA mode: use PostgreSQL-backed distributed locking.
215
226
  // If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
216
227
  const haConfig = {
217
228
  ...config,
218
229
  maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000,
219
230
  };
220
- const { signer } = await createHASigner(haConfig);
221
- validatorKeyStore = new HAKeyStore(validatorKeyStore, signer);
231
+ ({ signer: slashingProtectionSigner } = await createHASigner(haConfig, {
232
+ telemetryClient: telemetry,
233
+ dateProvider,
234
+ }));
235
+ } else {
236
+ // Single-node mode: use LMDB-backed local signing protection.
237
+ // This prevents double-signing if the node crashes and restarts mid-proposal.
238
+ ({ signer: slashingProtectionSigner } = await createLocalSignerWithProtection(config, {
239
+ telemetryClient: telemetry,
240
+ dateProvider,
241
+ }));
222
242
  }
243
+ const validatorKeyStore: ExtendedValidatorKeyStore = new HAKeyStore(nodeKeystoreAdapter, slashingProtectionSigner);
223
244
 
224
245
  const validator = new ValidatorClient(
225
246
  validatorKeyStore,
226
247
  epochCache,
227
248
  p2pClient,
228
- blockProposalHandler,
249
+ proposalHandler,
229
250
  blockSource,
230
251
  checkpointsBuilder,
231
252
  worldState,
232
253
  l1ToL2MessageSource,
233
254
  config,
234
255
  blobClient,
256
+ slashingProtectionSigner,
235
257
  dateProvider,
236
258
  telemetry,
237
259
  );
@@ -245,8 +267,8 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
245
267
  .filter(addr => !this.config.disabledValidators.some(disabled => disabled.equals(addr)));
246
268
  }
247
269
 
248
- public getBlockProposalHandler() {
249
- return this.blockProposalHandler;
270
+ public getProposalHandler() {
271
+ return this.proposalHandler;
250
272
  }
251
273
 
252
274
  public signWithAddress(addr: EthAddress, msg: TypedDataDefinition, context: SigningContext) {
@@ -269,6 +291,12 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
269
291
  this.config = { ...this.config, ...config };
270
292
  }
271
293
 
294
+ public reloadKeystore(newManager: KeystoreManager): void {
295
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
296
+ this.keyStore = new HAKeyStore(newAdapter, this.slashingProtectionSigner);
297
+ this.validationService = new ValidationService(this.keyStore, this.log.createChild('validation-service'));
298
+ }
299
+
272
300
  public async start() {
273
301
  if (this.epochCacheUpdateLoop.isRunning()) {
274
302
  this.log.warn(`Validator client already started`);
@@ -313,7 +341,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
313
341
  checkpoint: CheckpointProposalCore,
314
342
  proposalSender: PeerId,
315
343
  ): Promise<CheckpointAttestation[] | undefined> => this.attestToCheckpointProposal(checkpoint, proposalSender);
316
- this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
344
+ this.p2pClient.registerValidatorCheckpointProposalHandler(checkpointHandler);
317
345
 
318
346
  // Duplicate proposal handler - triggers slashing for equivocation
319
347
  this.p2pClient.registerDuplicateProposalCallback((info: DuplicateProposalInfo) => {
@@ -352,13 +380,12 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
352
380
  return false;
353
381
  }
354
382
 
355
- // Ignore proposals from ourselves (may happen in HA setups)
383
+ // Log self-proposals from HA peers (same validator key on different nodes)
356
384
  if (this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
357
- this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
385
+ this.log.verbose(`Processing block proposal from HA peer for slot ${slotNumber}`, {
358
386
  proposer: proposer.toString(),
359
387
  slotNumber,
360
388
  });
361
- return false;
362
389
  }
363
390
 
364
391
  // Check if we're in the committee (for metrics purposes)
@@ -374,25 +401,25 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
374
401
 
375
402
  // Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
376
403
  // In fisherman mode, we always reexecute to validate proposals.
377
- const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } =
378
- this.config;
404
+ const { slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } = this.config;
379
405
  const shouldReexecute =
380
406
  fishermanMode ||
381
- (slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute) ||
382
- (partOfCommittee && validatorReexecute) ||
407
+ slashBroadcastedInvalidBlockPenalty > 0n ||
408
+ partOfCommittee ||
383
409
  alwaysReexecuteBlockProposals ||
384
410
  this.blobClient.canUpload();
385
411
 
386
- const validationResult = await this.blockProposalHandler.handleBlockProposal(
412
+ const validationResult = await this.proposalHandler.handleBlockProposal(
387
413
  proposal,
388
414
  proposalSender,
389
415
  !!shouldReexecute && !escapeHatchOpen,
390
416
  );
391
417
 
392
418
  if (!validationResult.isValid) {
393
- this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
394
-
395
419
  const reason = validationResult.reason || 'unknown';
420
+
421
+ this.log.warn(`Block proposal validation failed: ${reason}`, proposalInfo);
422
+
396
423
  // Classify failure reason: bad proposal vs node issue
397
424
  const badProposalReasons: BlockProposalValidationFailureReason[] = [
398
425
  'invalid_proposal',
@@ -447,60 +474,51 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
447
474
  proposal: CheckpointProposalCore,
448
475
  _proposalSender: PeerId,
449
476
  ): Promise<CheckpointAttestation[] | undefined> {
450
- const slotNumber = proposal.slotNumber;
477
+ const proposalSlotNumber = proposal.slotNumber;
451
478
  const proposer = proposal.getSender();
452
479
 
453
480
  // If escape hatch is open for this slot's epoch, do not attest.
454
- if (await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber)) {
455
- this.log.warn(`Escape hatch open for slot ${slotNumber}, skipping checkpoint attestation handling`);
456
- return undefined;
457
- }
458
-
459
- // Reject proposals with invalid signatures
460
- if (!proposer) {
461
- this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
481
+ if (await this.epochCache.isEscapeHatchOpenAtSlot(proposalSlotNumber)) {
482
+ this.log.warn(`Escape hatch open for slot ${proposalSlotNumber}, skipping checkpoint attestation handling`);
462
483
  return undefined;
463
484
  }
464
485
 
465
486
  // Ignore proposals from ourselves (may happen in HA setups)
466
- if (this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
467
- this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
487
+ if (proposer && this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
488
+ this.log.debug(`Ignoring block proposal from self for slot ${proposalSlotNumber}`, {
468
489
  proposer: proposer.toString(),
469
- slotNumber,
490
+ proposalSlotNumber,
470
491
  });
471
492
  return undefined;
472
493
  }
473
494
 
474
- // Check that I have any address in current committee before attesting
475
- const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
495
+ // Check that I have any address in the committee where this checkpoint will land before attesting
496
+ const inCommittee = await this.epochCache.filterInCommittee(proposalSlotNumber, this.getValidatorAddresses());
476
497
  const partOfCommittee = inCommittee.length > 0;
477
498
 
478
499
  const proposalInfo = {
479
- slotNumber,
500
+ proposalSlotNumber,
480
501
  archive: proposal.archive.toString(),
481
- proposer: proposer.toString(),
482
- txCount: proposal.txHashes.length,
502
+ proposer: proposer?.toString(),
483
503
  };
484
- this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
504
+ this.log.info(`Received checkpoint proposal for slot ${proposalSlotNumber}`, {
485
505
  ...proposalInfo,
486
- txHashes: proposal.txHashes.map(t => t.toString()),
487
506
  fishermanMode: this.config.fishermanMode || false,
488
507
  });
489
508
 
490
- // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
509
+ // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set).
510
+ // Uses the cached result from the all-nodes callback if available (avoids double validation).
511
+ let checkpointNumber: CheckpointNumber;
491
512
  if (this.config.skipCheckpointProposalValidation) {
492
- this.log.warn(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
513
+ this.log.warn(`Skipping checkpoint proposal validation for slot ${proposalSlotNumber}`, proposalInfo);
514
+ checkpointNumber = CheckpointNumber(0);
493
515
  } else {
494
- const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
516
+ const validationResult = await this.proposalHandler.handleCheckpointProposal(proposal, proposalInfo);
495
517
  if (!validationResult.isValid) {
496
518
  this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
497
519
  return undefined;
498
520
  }
499
- }
500
-
501
- // Upload blobs to filestore if we can (fire and forget)
502
- if (this.blobClient.canUpload()) {
503
- void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
521
+ checkpointNumber = validationResult.checkpointNumber;
504
522
  }
505
523
 
506
524
  // Check that I have any address in current committee before attesting
@@ -511,14 +529,28 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
511
529
  }
512
530
 
513
531
  // Provided all of the above checks pass, we can attest to the proposal
514
- this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
515
- ...proposalInfo,
516
- inCommittee: partOfCommittee,
517
- fishermanMode: this.config.fishermanMode || false,
518
- });
532
+ this.log.info(
533
+ `${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${proposalSlotNumber}`,
534
+ {
535
+ ...proposalInfo,
536
+ inCommittee: partOfCommittee,
537
+ fishermanMode: this.config.fishermanMode || false,
538
+ },
539
+ );
519
540
 
520
541
  this.metrics.incSuccessfulAttestations(inCommittee.length);
521
542
 
543
+ // Track epoch participation per attester: count each (attester, epoch) pair at most once
544
+ const proposalEpoch = getEpochAtSlot(proposalSlotNumber, this.epochCache.getL1Constants());
545
+ for (const attester of inCommittee) {
546
+ const key = attester.toString();
547
+ const lastEpoch = this.lastAttestedEpochByAttester.get(key);
548
+ if (lastEpoch === undefined || proposalEpoch > lastEpoch) {
549
+ this.lastAttestedEpochByAttester.set(key, proposalEpoch);
550
+ this.metrics.incAttestedEpochCount(attester);
551
+ }
552
+ }
553
+
522
554
  // Determine which validators should attest
523
555
  let attestors: EthAddress[];
524
556
  if (partOfCommittee) {
@@ -537,14 +569,14 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
537
569
 
538
570
  if (this.config.fishermanMode) {
539
571
  // bail out early and don't save attestations to the pool in fisherman mode
540
- this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
572
+ this.log.info(`Creating checkpoint attestations for slot ${proposalSlotNumber}`, {
541
573
  ...proposalInfo,
542
574
  attestors: attestors.map(a => a.toString()),
543
575
  });
544
576
  return undefined;
545
577
  }
546
578
 
547
- return await this.createCheckpointAttestationsFromProposal(proposal, attestors);
579
+ return await this.createCheckpointAttestationsFromProposal(proposal, attestors, checkpointNumber);
548
580
  }
549
581
 
550
582
  /**
@@ -571,13 +603,14 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
571
603
  private async createCheckpointAttestationsFromProposal(
572
604
  proposal: CheckpointProposalCore,
573
605
  attestors: EthAddress[] = [],
606
+ checkpointNumber: CheckpointNumber,
574
607
  ): Promise<CheckpointAttestation[] | undefined> {
575
608
  // Equivocation check: must happen right before signing to minimize the race window
576
609
  if (!this.shouldAttestToSlot(proposal.slotNumber)) {
577
610
  return undefined;
578
611
  }
579
612
 
580
- const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
613
+ const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors, checkpointNumber);
581
614
 
582
615
  // Track the proposal we attested to (to prevent equivocation)
583
616
  this.lastAttestedProposal = proposal;
@@ -586,153 +619,10 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
586
619
  return attestations;
587
620
  }
588
621
 
589
- /**
590
- * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
591
- * @returns Validation result with isValid flag and reason if invalid.
592
- */
593
- private async validateCheckpointProposal(
594
- proposal: CheckpointProposalCore,
595
- proposalInfo: LogData,
596
- ): Promise<{ isValid: true } | { isValid: false; reason: string }> {
597
- const slot = proposal.slotNumber;
598
- const timeoutSeconds = 10; // TODO(palla/mbps): This should map to the timetable settings
599
-
600
- // Wait for last block to sync by archive
601
- let lastBlockHeader: BlockHeader | undefined;
602
- try {
603
- lastBlockHeader = await retryUntil(
604
- async () => {
605
- await this.blockSource.syncImmediate();
606
- return this.blockSource.getBlockHeaderByArchive(proposal.archive);
607
- },
608
- `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`,
609
- timeoutSeconds,
610
- 0.5,
611
- );
612
- } catch (err) {
613
- if (err instanceof TimeoutError) {
614
- this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
615
- return { isValid: false, reason: 'last_block_not_found' };
616
- }
617
- this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
618
- return { isValid: false, reason: 'block_fetch_error' };
619
- }
620
-
621
- if (!lastBlockHeader) {
622
- this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
623
- return { isValid: false, reason: 'last_block_not_found' };
624
- }
625
-
626
- // Get all full blocks for the slot and checkpoint
627
- const blocks = await this.blockSource.getBlocksForSlot(slot);
628
- if (blocks.length === 0) {
629
- this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
630
- return { isValid: false, reason: 'no_blocks_for_slot' };
631
- }
632
-
633
- this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
634
- ...proposalInfo,
635
- blockNumbers: blocks.map(b => b.number),
636
- });
637
-
638
- // Get checkpoint constants from first block
639
- const firstBlock = blocks[0];
640
- const constants = this.extractCheckpointConstants(firstBlock);
641
- const checkpointNumber = firstBlock.checkpointNumber;
642
-
643
- // Get L1-to-L2 messages for this checkpoint
644
- const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
645
-
646
- // Compute the previous checkpoint out hashes for the epoch.
647
- // TODO: There can be a more efficient way to get the previous checkpoint out hashes without having to fetch the
648
- // actual checkpoints and the blocks/txs in them.
649
- const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
650
- const previousCheckpoints = (await this.blockSource.getCheckpointsForEpoch(epoch))
651
- .filter(b => b.number < checkpointNumber)
652
- .sort((a, b) => a.number - b.number);
653
- const previousCheckpointOutHashes = previousCheckpoints.map(c => c.getCheckpointOutHash());
654
-
655
- // Fork world state at the block before the first block
656
- const parentBlockNumber = BlockNumber(firstBlock.number - 1);
657
- const fork = await this.worldState.fork(parentBlockNumber);
658
-
659
- try {
660
- // Create checkpoint builder with all existing blocks
661
- const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
662
- checkpointNumber,
663
- constants,
664
- l1ToL2Messages,
665
- previousCheckpointOutHashes,
666
- fork,
667
- blocks,
668
- this.log.getBindings(),
669
- );
670
-
671
- // Complete the checkpoint to get computed values
672
- const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
673
-
674
- // Compare checkpoint header with proposal
675
- if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
676
- this.log.warn(`Checkpoint header mismatch`, {
677
- ...proposalInfo,
678
- computed: computedCheckpoint.header.toInspect(),
679
- proposal: proposal.checkpointHeader.toInspect(),
680
- });
681
- return { isValid: false, reason: 'checkpoint_header_mismatch' };
682
- }
683
-
684
- // Compare archive root with proposal
685
- if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
686
- this.log.warn(`Archive root mismatch`, {
687
- ...proposalInfo,
688
- computed: computedCheckpoint.archive.root.toString(),
689
- proposal: proposal.archive.toString(),
690
- });
691
- return { isValid: false, reason: 'archive_mismatch' };
692
- }
693
-
694
- // Check that the accumulated epoch out hash matches the value in the proposal.
695
- // The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
696
- const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
697
- const computedEpochOutHash = accumulateCheckpointOutHashes([...previousCheckpointOutHashes, checkpointOutHash]);
698
- const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
699
- if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
700
- this.log.warn(`Epoch out hash mismatch`, {
701
- proposalEpochOutHash: proposalEpochOutHash.toString(),
702
- computedEpochOutHash: computedEpochOutHash.toString(),
703
- checkpointOutHash: checkpointOutHash.toString(),
704
- previousCheckpointOutHashes: previousCheckpointOutHashes.map(h => h.toString()),
705
- ...proposalInfo,
706
- });
707
- return { isValid: false, reason: 'out_hash_mismatch' };
708
- }
709
-
710
- this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
711
- return { isValid: true };
712
- } finally {
713
- await fork.close();
714
- }
715
- }
716
-
717
- /**
718
- * Extract checkpoint global variables from a block.
719
- */
720
- private extractCheckpointConstants(block: L2Block): CheckpointGlobalVariables {
721
- const gv = block.header.globalVariables;
722
- return {
723
- chainId: gv.chainId,
724
- version: gv.version,
725
- slotNumber: gv.slotNumber,
726
- coinbase: gv.coinbase,
727
- feeRecipient: gv.feeRecipient,
728
- gasFees: gv.gasFees,
729
- };
730
- }
731
-
732
622
  /**
733
623
  * Uploads blobs for a checkpoint to the filestore (fire and forget).
734
624
  */
735
- private async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
625
+ protected async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
736
626
  try {
737
627
  const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
738
628
  if (!lastBlockHeader) {
@@ -747,7 +637,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
747
637
  }
748
638
 
749
639
  const blobFields = blocks.flatMap(b => b.toBlobFields());
750
- const blobs: Blob[] = getBlobsPerL1Block(blobFields);
640
+ const blobs: Blob[] = await getBlobsPerL1Block(blobFields);
751
641
  await this.blobClient.sendBlobsToFilestore(blobs);
752
642
  this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
753
643
  ...proposalInfo,
@@ -833,6 +723,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
833
723
 
834
724
  async createBlockProposal(
835
725
  blockHeader: BlockHeader,
726
+ checkpointNumber: CheckpointNumber,
836
727
  indexWithinCheckpoint: IndexWithinCheckpoint,
837
728
  inHash: Fr,
838
729
  archive: Fr,
@@ -859,6 +750,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
859
750
  );
860
751
  const newProposal = await this.validationService.createBlockProposal(
861
752
  blockHeader,
753
+ checkpointNumber,
862
754
  indexWithinCheckpoint,
863
755
  inHash,
864
756
  archive,
@@ -876,7 +768,9 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
876
768
  async createCheckpointProposal(
877
769
  checkpointHeader: CheckpointHeader,
878
770
  archive: Fr,
879
- lastBlockInfo: CreateCheckpointProposalLastBlockData | undefined,
771
+ checkpointNumber: CheckpointNumber,
772
+ feeAssetPriceModifier: bigint,
773
+ lastBlockProposal: BlockProposal | undefined,
880
774
  proposerAddress: EthAddress | undefined,
881
775
  options: CheckpointProposalOptions = {},
882
776
  ): Promise<CheckpointProposal> {
@@ -897,7 +791,9 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
897
791
  const newProposal = await this.validationService.createCheckpointProposal(
898
792
  checkpointHeader,
899
793
  archive,
900
- lastBlockInfo,
794
+ checkpointNumber,
795
+ feeAssetPriceModifier,
796
+ lastBlockProposal,
901
797
  proposerAddress,
902
798
  options,
903
799
  );
@@ -913,16 +809,24 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
913
809
  attestationsAndSigners: CommitteeAttestationsAndSigners,
914
810
  proposer: EthAddress,
915
811
  slot: SlotNumber,
916
- blockNumber: BlockNumber | CheckpointNumber,
812
+ checkpointNumber: CheckpointNumber,
917
813
  ): Promise<Signature> {
918
- return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber);
814
+ return await this.validationService.signAttestationsAndSigners(
815
+ attestationsAndSigners,
816
+ proposer,
817
+ slot,
818
+ checkpointNumber,
819
+ );
919
820
  }
920
821
 
921
- async collectOwnAttestations(proposal: CheckpointProposal): Promise<CheckpointAttestation[]> {
822
+ async collectOwnAttestations(
823
+ proposal: CheckpointProposal,
824
+ checkpointNumber: CheckpointNumber,
825
+ ): Promise<CheckpointAttestation[]> {
922
826
  const slot = proposal.slotNumber;
923
827
  const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
924
828
  this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, { inCommittee });
925
- const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
829
+ const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee, checkpointNumber);
926
830
 
927
831
  if (!attestations) {
928
832
  return [];
@@ -941,6 +845,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
941
845
  proposal: CheckpointProposal,
942
846
  required: number,
943
847
  deadline: Date,
848
+ checkpointNumber: CheckpointNumber,
944
849
  ): Promise<CheckpointAttestation[]> {
945
850
  // Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
946
851
  const slot = proposal.slotNumber;
@@ -953,7 +858,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
953
858
  throw new AttestationTimeoutError(0, required, slot);
954
859
  }
955
860
 
956
- await this.collectOwnAttestations(proposal);
861
+ await this.collectOwnAttestations(proposal, checkpointNumber);
957
862
 
958
863
  const proposalId = proposal.archive.toString();
959
864
  const myAddresses = this.getValidatorAddresses();