@aztec/validator-client 0.0.1-commit.9d2bcf6d → 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.
Files changed (51) hide show
  1. package/README.md +60 -18
  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 +26 -6
  8. package/dest/duties/validation_service.d.ts +2 -2
  9. package/dest/duties/validation_service.d.ts.map +1 -1
  10. package/dest/duties/validation_service.js +6 -12
  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 +10 -2
  19. package/dest/metrics.d.ts.map +1 -1
  20. package/dest/metrics.js +12 -0
  21. package/dest/proposal_handler.d.ts +94 -0
  22. package/dest/proposal_handler.d.ts.map +1 -0
  23. package/dest/{block_proposal_handler.js → proposal_handler.js} +377 -67
  24. package/dest/validator.d.ts +35 -21
  25. package/dest/validator.d.ts.map +1 -1
  26. package/dest/validator.js +177 -218
  27. package/package.json +19 -19
  28. package/src/checkpoint_builder.ts +142 -39
  29. package/src/config.ts +26 -6
  30. package/src/duties/validation_service.ts +12 -11
  31. package/src/factory.ts +9 -3
  32. package/src/index.ts +1 -2
  33. package/src/key_store/ha_key_store.ts +1 -1
  34. package/src/metrics.ts +19 -1
  35. package/src/proposal_handler.ts +907 -0
  36. package/src/validator.ts +240 -248
  37. package/dest/block_proposal_handler.d.ts +0 -63
  38. package/dest/block_proposal_handler.d.ts.map +0 -1
  39. package/dest/tx_validator/index.d.ts +0 -3
  40. package/dest/tx_validator/index.d.ts.map +0 -1
  41. package/dest/tx_validator/index.js +0 -2
  42. package/dest/tx_validator/nullifier_cache.d.ts +0 -14
  43. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  44. package/dest/tx_validator/nullifier_cache.js +0 -24
  45. package/dest/tx_validator/tx_validator_factory.d.ts +0 -19
  46. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  47. package/dest/tx_validator/tx_validator_factory.js +0 -54
  48. package/src/block_proposal_handler.ts +0 -555
  49. package/src/tx_validator/index.ts +0 -2
  50. package/src/tx_validator/nullifier_cache.ts +0 -30
  51. package/src/tx_validator/tx_validator_factory.ts +0 -154
package/src/validator.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import type { BlobClientInterface } from '@aztec/blob-client/client';
2
- import { type Blob, getBlobsPerL1Block } from '@aztec/blob-lib';
3
2
  import type { EpochCache } from '@aztec/epoch-cache';
4
3
  import {
5
4
  BlockNumber,
@@ -9,20 +8,18 @@ import {
9
8
  SlotNumber,
10
9
  } from '@aztec/foundation/branded-types';
11
10
  import { Fr } from '@aztec/foundation/curves/bn254';
12
- import { TimeoutError } from '@aztec/foundation/error';
13
11
  import type { EthAddress } from '@aztec/foundation/eth-address';
14
12
  import type { Signature } from '@aztec/foundation/eth-signature';
15
- import { type LogData, type Logger, createLogger } from '@aztec/foundation/log';
16
- import { retryUntil } from '@aztec/foundation/retry';
13
+ import { type Logger, createLogger } from '@aztec/foundation/log';
17
14
  import { RunningPromise } from '@aztec/foundation/running-promise';
18
15
  import { sleep } from '@aztec/foundation/sleep';
19
16
  import { DateProvider } from '@aztec/foundation/timer';
20
17
  import type { KeystoreManager } from '@aztec/node-keystore';
21
- import type { P2P, PeerId } from '@aztec/p2p';
18
+ import type { DuplicateAttestationInfo, DuplicateProposalInfo, P2P, PeerId } from '@aztec/p2p';
22
19
  import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
23
20
  import { OffenseType, WANT_TO_SLASH_EVENT, type Watcher, type WatcherEmitter } from '@aztec/slasher';
24
21
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
25
- import type { CommitteeAttestationsAndSigners, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
22
+ import type { CommitteeAttestationsAndSigners, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
26
23
  import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
27
24
  import type {
28
25
  CreateCheckpointProposalLastBlockData,
@@ -31,32 +28,37 @@ import type {
31
28
  ValidatorClientFullConfig,
32
29
  WorldStateSynchronizer,
33
30
  } from '@aztec/stdlib/interfaces/server';
34
- import { type L1ToL2MessageSource, accumulateCheckpointOutHashes } from '@aztec/stdlib/messaging';
35
- import type {
36
- BlockProposal,
37
- BlockProposalOptions,
38
- CheckpointAttestation,
39
- CheckpointProposalCore,
40
- CheckpointProposalOptions,
31
+ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
32
+ import {
33
+ type BlockProposal,
34
+ type BlockProposalOptions,
35
+ type CheckpointAttestation,
36
+ CheckpointProposal,
37
+ type CheckpointProposalCore,
38
+ type CheckpointProposalOptions,
41
39
  } from '@aztec/stdlib/p2p';
42
- import { CheckpointProposal } from '@aztec/stdlib/p2p';
43
40
  import type { CheckpointHeader } from '@aztec/stdlib/rollup';
44
- import type { BlockHeader, CheckpointGlobalVariables, Tx } from '@aztec/stdlib/tx';
41
+ import type { BlockHeader, Tx } from '@aztec/stdlib/tx';
45
42
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
46
43
  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';
44
+ import {
45
+ createHASigner,
46
+ createLocalSignerWithProtection,
47
+ createSignerFromSharedDb,
48
+ } from '@aztec/validator-ha-signer/factory';
49
+ import { DutyType, type SigningContext, type SlashingProtectionDatabase } from '@aztec/validator-ha-signer/types';
50
+ import type { ValidatorHASigner } from '@aztec/validator-ha-signer/validator-ha-signer';
49
51
 
50
52
  import { EventEmitter } from 'events';
51
53
  import type { TypedDataDefinition } from 'viem';
52
54
 
53
- import { BlockProposalHandler, type BlockProposalValidationFailureReason } from './block_proposal_handler.js';
54
55
  import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
55
56
  import { ValidationService } from './duties/validation_service.js';
56
57
  import { HAKeyStore } from './key_store/ha_key_store.js';
57
58
  import type { ExtendedValidatorKeyStore } from './key_store/interface.js';
58
59
  import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
59
60
  import { ValidatorMetrics } from './metrics.js';
61
+ import { type BlockProposalValidationFailureReason, ProposalHandler } from './proposal_handler.js';
60
62
 
61
63
  // We maintain a set of proposers who have proposed invalid blocks.
62
64
  // Just cap the set to avoid unbounded growth.
@@ -76,29 +78,33 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
76
78
  private validationService: ValidationService;
77
79
  private metrics: ValidatorMetrics;
78
80
  private log: Logger;
79
-
80
81
  // Whether it has already registered handlers on the p2p client
81
82
  private hasRegisteredHandlers = false;
82
83
 
83
- // Used to check if we are sending the same proposal twice
84
- private previousProposal?: BlockProposal;
84
+ /** Tracks the last block proposal we created, to detect duplicate proposal attempts. */
85
+ private lastProposedBlock?: BlockProposal;
86
+
87
+ /** Tracks the last checkpoint proposal we created. */
88
+ private lastProposedCheckpoint?: CheckpointProposal;
85
89
 
86
90
  private lastEpochForCommitteeUpdateLoop: EpochNumber | undefined;
87
91
  private epochCacheUpdateLoop: RunningPromise;
92
+ /** Tracks the last epoch in which each attester successfully submitted at least one attestation. */
93
+ private lastAttestedEpochByAttester: Map<string, EpochNumber> = new Map();
88
94
 
89
95
  private proposersOfInvalidBlocks: Set<string> = new Set();
90
96
 
97
+ /** Tracks the last checkpoint proposal we attested to, to prevent equivocation. */
98
+ private lastAttestedProposal?: CheckpointProposalCore;
99
+
91
100
  protected constructor(
92
101
  private keyStore: ExtendedValidatorKeyStore,
93
102
  private epochCache: EpochCache,
94
103
  private p2pClient: P2P,
95
- private blockProposalHandler: BlockProposalHandler,
96
- private blockSource: L2BlockSource,
97
- private checkpointsBuilder: FullNodeCheckpointsBuilder,
98
- private worldState: WorldStateSynchronizer,
99
- private l1ToL2MessageSource: L1ToL2MessageSource,
104
+ private proposalHandler: ProposalHandler,
100
105
  private config: ValidatorClientFullConfig,
101
106
  private blobClient: BlobClientInterface,
107
+ private slashingProtectionSigner: ValidatorHASigner,
102
108
  private dateProvider: DateProvider = new DateProvider(),
103
109
  telemetry: TelemetryClient = getTelemetryClient(),
104
110
  log = createLogger('validator'),
@@ -152,6 +158,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
152
158
  this.log.trace(`No committee found for slot`);
153
159
  return;
154
160
  }
161
+ this.metrics.setCurrentEpoch(epoch);
155
162
  if (epoch !== this.lastEpochForCommitteeUpdateLoop) {
156
163
  const me = this.getValidatorAddresses();
157
164
  const committeeSet = new Set(committee.map(v => v.toString()));
@@ -185,12 +192,14 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
185
192
  blobClient: BlobClientInterface,
186
193
  dateProvider: DateProvider = new DateProvider(),
187
194
  telemetry: TelemetryClient = getTelemetryClient(),
195
+ slashingProtectionDb?: SlashingProtectionDatabase,
188
196
  ) {
189
197
  const metrics = new ValidatorMetrics(telemetry);
190
198
  const blockProposalValidator = new BlockProposalValidator(epochCache, {
191
199
  txsPermitted: !config.disableTransactions,
200
+ maxTxsPerBlock: config.validateMaxTxsPerBlock,
192
201
  });
193
- const blockProposalHandler = new BlockProposalHandler(
202
+ const proposalHandler = new ProposalHandler(
194
203
  checkpointsBuilder,
195
204
  worldState,
196
205
  blockSource,
@@ -199,33 +208,49 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
199
208
  blockProposalValidator,
200
209
  epochCache,
201
210
  config,
211
+ blobClient,
202
212
  metrics,
203
213
  dateProvider,
204
214
  telemetry,
205
215
  );
206
216
 
207
- let validatorKeyStore: ExtendedValidatorKeyStore = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
208
- if (config.haSigningEnabled) {
217
+ const nodeKeystoreAdapter = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
218
+ let slashingProtectionSigner: ValidatorHASigner;
219
+ if (slashingProtectionDb) {
220
+ // Shared database mode: use a pre-existing database (e.g. for testing HA setups).
221
+ ({ signer: slashingProtectionSigner } = createSignerFromSharedDb(slashingProtectionDb, config, {
222
+ telemetryClient: telemetry,
223
+ dateProvider,
224
+ }));
225
+ } else if (config.haSigningEnabled) {
226
+ // Multi-node HA mode: use PostgreSQL-backed distributed locking.
209
227
  // If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
210
228
  const haConfig = {
211
229
  ...config,
212
230
  maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000,
213
231
  };
214
- const { signer } = await createHASigner(haConfig);
215
- validatorKeyStore = new HAKeyStore(validatorKeyStore, signer);
232
+ ({ signer: slashingProtectionSigner } = await createHASigner(haConfig, {
233
+ telemetryClient: telemetry,
234
+ dateProvider,
235
+ }));
236
+ } else {
237
+ // Single-node mode: use LMDB-backed local signing protection.
238
+ // This prevents double-signing if the node crashes and restarts mid-proposal.
239
+ ({ signer: slashingProtectionSigner } = await createLocalSignerWithProtection(config, {
240
+ telemetryClient: telemetry,
241
+ dateProvider,
242
+ }));
216
243
  }
244
+ const validatorKeyStore: ExtendedValidatorKeyStore = new HAKeyStore(nodeKeystoreAdapter, slashingProtectionSigner);
217
245
 
218
246
  const validator = new ValidatorClient(
219
247
  validatorKeyStore,
220
248
  epochCache,
221
249
  p2pClient,
222
- blockProposalHandler,
223
- blockSource,
224
- checkpointsBuilder,
225
- worldState,
226
- l1ToL2MessageSource,
250
+ proposalHandler,
227
251
  config,
228
252
  blobClient,
253
+ slashingProtectionSigner,
229
254
  dateProvider,
230
255
  telemetry,
231
256
  );
@@ -239,8 +264,8 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
239
264
  .filter(addr => !this.config.disabledValidators.some(disabled => disabled.equals(addr)));
240
265
  }
241
266
 
242
- public getBlockProposalHandler() {
243
- return this.blockProposalHandler;
267
+ public getProposalHandler() {
268
+ return this.proposalHandler;
244
269
  }
245
270
 
246
271
  public signWithAddress(addr: EthAddress, msg: TypedDataDefinition, context: SigningContext) {
@@ -263,6 +288,12 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
263
288
  this.config = { ...this.config, ...config };
264
289
  }
265
290
 
291
+ public reloadKeystore(newManager: KeystoreManager): void {
292
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
293
+ this.keyStore = new HAKeyStore(newAdapter, this.slashingProtectionSigner);
294
+ this.validationService = new ValidationService(this.keyStore, this.log.createChild('validation-service'));
295
+ }
296
+
266
297
  public async start() {
267
298
  if (this.epochCacheUpdateLoop.isRunning()) {
268
299
  this.log.warn(`Validator client already started`);
@@ -309,6 +340,16 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
309
340
  ): Promise<CheckpointAttestation[] | undefined> => this.attestToCheckpointProposal(checkpoint, proposalSender);
310
341
  this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
311
342
 
343
+ // Duplicate proposal handler - triggers slashing for equivocation
344
+ this.p2pClient.registerDuplicateProposalCallback((info: DuplicateProposalInfo) => {
345
+ this.handleDuplicateProposal(info);
346
+ });
347
+
348
+ // Duplicate attestation handler - triggers slashing for attestation equivocation
349
+ this.p2pClient.registerDuplicateAttestationCallback((info: DuplicateAttestationInfo) => {
350
+ this.handleDuplicateAttestation(info);
351
+ });
352
+
312
353
  const myAddresses = this.getValidatorAddresses();
313
354
  this.p2pClient.registerThisValidatorAddresses(myAddresses);
314
355
 
@@ -336,6 +377,14 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
336
377
  return false;
337
378
  }
338
379
 
380
+ // Log self-proposals from HA peers (same validator key on different nodes)
381
+ if (this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
382
+ this.log.verbose(`Processing block proposal from HA peer for slot ${slotNumber}`, {
383
+ proposer: proposer.toString(),
384
+ slotNumber,
385
+ });
386
+ }
387
+
339
388
  // Check if we're in the committee (for metrics purposes)
340
389
  const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
341
390
  const partOfCommittee = inCommittee.length > 0;
@@ -349,25 +398,25 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
349
398
 
350
399
  // Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
351
400
  // In fisherman mode, we always reexecute to validate proposals.
352
- const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } =
353
- this.config;
401
+ const { slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } = this.config;
354
402
  const shouldReexecute =
355
403
  fishermanMode ||
356
- (slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute) ||
357
- (partOfCommittee && validatorReexecute) ||
404
+ slashBroadcastedInvalidBlockPenalty > 0n ||
405
+ partOfCommittee ||
358
406
  alwaysReexecuteBlockProposals ||
359
407
  this.blobClient.canUpload();
360
408
 
361
- const validationResult = await this.blockProposalHandler.handleBlockProposal(
409
+ const validationResult = await this.proposalHandler.handleBlockProposal(
362
410
  proposal,
363
411
  proposalSender,
364
412
  !!shouldReexecute && !escapeHatchOpen,
365
413
  );
366
414
 
367
415
  if (!validationResult.isValid) {
368
- this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
369
-
370
416
  const reason = validationResult.reason || 'unknown';
417
+
418
+ this.log.warn(`Block proposal validation failed: ${reason}`, proposalInfo);
419
+
371
420
  // Classify failure reason: bad proposal vs node issue
372
421
  const badProposalReasons: BlockProposalValidationFailureReason[] = [
373
422
  'invalid_proposal',
@@ -422,53 +471,49 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
422
471
  proposal: CheckpointProposalCore,
423
472
  _proposalSender: PeerId,
424
473
  ): Promise<CheckpointAttestation[] | undefined> {
425
- const slotNumber = proposal.slotNumber;
474
+ const proposalSlotNumber = proposal.slotNumber;
426
475
  const proposer = proposal.getSender();
427
476
 
428
477
  // If escape hatch is open for this slot's epoch, do not attest.
429
- if (await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber)) {
430
- this.log.warn(`Escape hatch open for slot ${slotNumber}, skipping checkpoint attestation handling`);
478
+ if (await this.epochCache.isEscapeHatchOpenAtSlot(proposalSlotNumber)) {
479
+ this.log.warn(`Escape hatch open for slot ${proposalSlotNumber}, skipping checkpoint attestation handling`);
431
480
  return undefined;
432
481
  }
433
482
 
434
- // Reject proposals with invalid signatures
435
- if (!proposer) {
436
- this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
483
+ // Ignore proposals from ourselves (may happen in HA setups)
484
+ if (proposer && this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
485
+ this.log.debug(`Ignoring block proposal from self for slot ${proposalSlotNumber}`, {
486
+ proposer: proposer.toString(),
487
+ proposalSlotNumber,
488
+ });
437
489
  return undefined;
438
490
  }
439
491
 
440
- // Check that I have any address in current committee before attesting
441
- const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
492
+ // Check that I have any address in the committee where this checkpoint will land before attesting
493
+ const inCommittee = await this.epochCache.filterInCommittee(proposalSlotNumber, this.getValidatorAddresses());
442
494
  const partOfCommittee = inCommittee.length > 0;
443
495
 
444
496
  const proposalInfo = {
445
- slotNumber,
497
+ proposalSlotNumber,
446
498
  archive: proposal.archive.toString(),
447
- proposer: proposer.toString(),
448
- txCount: proposal.txHashes.length,
499
+ proposer: proposer?.toString(),
449
500
  };
450
- this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
501
+ this.log.info(`Received checkpoint proposal for slot ${proposalSlotNumber}`, {
451
502
  ...proposalInfo,
452
- txHashes: proposal.txHashes.map(t => t.toString()),
453
503
  fishermanMode: this.config.fishermanMode || false,
454
504
  });
455
505
 
456
- // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
506
+ // Validate the checkpoint proposal and upload blobs (unless skipCheckpointProposalValidation is set)
457
507
  if (this.config.skipCheckpointProposalValidation) {
458
- this.log.warn(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
508
+ this.log.warn(`Skipping checkpoint proposal validation for slot ${proposalSlotNumber}`, proposalInfo);
459
509
  } else {
460
- const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
510
+ const validationResult = await this.proposalHandler.handleCheckpointProposal(proposal, proposalInfo);
461
511
  if (!validationResult.isValid) {
462
512
  this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
463
513
  return undefined;
464
514
  }
465
515
  }
466
516
 
467
- // Upload blobs to filestore if we can (fire and forget)
468
- if (this.blobClient.canUpload()) {
469
- void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
470
- }
471
-
472
517
  // Check that I have any address in current committee before attesting
473
518
  // In fisherman mode, we still create attestations for validation even if not in committee
474
519
  if (!partOfCommittee && !this.config.fishermanMode) {
@@ -477,14 +522,28 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
477
522
  }
478
523
 
479
524
  // Provided all of the above checks pass, we can attest to the proposal
480
- this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
481
- ...proposalInfo,
482
- inCommittee: partOfCommittee,
483
- fishermanMode: this.config.fishermanMode || false,
484
- });
525
+ this.log.info(
526
+ `${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${proposalSlotNumber}`,
527
+ {
528
+ ...proposalInfo,
529
+ inCommittee: partOfCommittee,
530
+ fishermanMode: this.config.fishermanMode || false,
531
+ },
532
+ );
485
533
 
486
534
  this.metrics.incSuccessfulAttestations(inCommittee.length);
487
535
 
536
+ // Track epoch participation per attester: count each (attester, epoch) pair at most once
537
+ const proposalEpoch = getEpochAtSlot(proposalSlotNumber, this.epochCache.getL1Constants());
538
+ for (const attester of inCommittee) {
539
+ const key = attester.toString();
540
+ const lastEpoch = this.lastAttestedEpochByAttester.get(key);
541
+ if (lastEpoch === undefined || proposalEpoch > lastEpoch) {
542
+ this.lastAttestedEpochByAttester.set(key, proposalEpoch);
543
+ this.metrics.incAttestedEpochCount(attester);
544
+ }
545
+ }
546
+
488
547
  // Determine which validators should attest
489
548
  let attestors: EthAddress[];
490
549
  if (partOfCommittee) {
@@ -503,195 +562,53 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
503
562
 
504
563
  if (this.config.fishermanMode) {
505
564
  // bail out early and don't save attestations to the pool in fisherman mode
506
- this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
565
+ this.log.info(`Creating checkpoint attestations for slot ${proposalSlotNumber}`, {
507
566
  ...proposalInfo,
508
567
  attestors: attestors.map(a => a.toString()),
509
568
  });
510
569
  return undefined;
511
570
  }
512
571
 
513
- return this.createCheckpointAttestationsFromProposal(proposal, attestors);
514
- }
515
-
516
- private async createCheckpointAttestationsFromProposal(
517
- proposal: CheckpointProposalCore,
518
- attestors: EthAddress[] = [],
519
- ): Promise<CheckpointAttestation[]> {
520
- const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
521
- await this.p2pClient.addCheckpointAttestations(attestations);
522
- return attestations;
572
+ return await this.createCheckpointAttestationsFromProposal(proposal, attestors);
523
573
  }
524
574
 
525
575
  /**
526
- * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
527
- * @returns Validation result with isValid flag and reason if invalid.
576
+ * Checks if we should attest to a slot based on equivocation prevention rules.
577
+ * @returns true if we should attest, false if we should skip
528
578
  */
529
- private async validateCheckpointProposal(
530
- proposal: CheckpointProposalCore,
531
- proposalInfo: LogData,
532
- ): Promise<{ isValid: true } | { isValid: false; reason: string }> {
533
- const slot = proposal.slotNumber;
534
- const timeoutSeconds = 10; // TODO(palla/mbps): This should map to the timetable settings
535
-
536
- // Wait for last block to sync by archive
537
- let lastBlockHeader: BlockHeader | undefined;
538
- try {
539
- lastBlockHeader = await retryUntil(
540
- async () => {
541
- await this.blockSource.syncImmediate();
542
- return this.blockSource.getBlockHeaderByArchive(proposal.archive);
543
- },
544
- `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`,
545
- timeoutSeconds,
546
- 0.5,
547
- );
548
- } catch (err) {
549
- if (err instanceof TimeoutError) {
550
- this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
551
- return { isValid: false, reason: 'last_block_not_found' };
552
- }
553
- this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
554
- return { isValid: false, reason: 'block_fetch_error' };
579
+ private shouldAttestToSlot(slotNumber: SlotNumber): boolean {
580
+ // If attestToEquivocatedProposals is true, always allow
581
+ if (this.config.attestToEquivocatedProposals) {
582
+ return true;
555
583
  }
556
584
 
557
- if (!lastBlockHeader) {
558
- this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
559
- return { isValid: false, reason: 'last_block_not_found' };
560
- }
561
-
562
- // Get all full blocks for the slot and checkpoint
563
- const blocks = await this.blockSource.getBlocksForSlot(slot);
564
- if (blocks.length === 0) {
565
- this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
566
- return { isValid: false, reason: 'no_blocks_for_slot' };
567
- }
568
-
569
- this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
570
- ...proposalInfo,
571
- blockNumbers: blocks.map(b => b.number),
572
- });
573
-
574
- // Get checkpoint constants from first block
575
- const firstBlock = blocks[0];
576
- const constants = this.extractCheckpointConstants(firstBlock);
577
- const checkpointNumber = firstBlock.checkpointNumber;
578
-
579
- // Get L1-to-L2 messages for this checkpoint
580
- const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
581
-
582
- // Compute the previous checkpoint out hashes for the epoch.
583
- // TODO: There can be a more efficient way to get the previous checkpoint out hashes without having to fetch the
584
- // actual checkpoints and the blocks/txs in them.
585
- const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
586
- const previousCheckpoints = (await this.blockSource.getCheckpointsForEpoch(epoch))
587
- .filter(b => b.number < checkpointNumber)
588
- .sort((a, b) => a.number - b.number);
589
- const previousCheckpointOutHashes = previousCheckpoints.map(c => c.getCheckpointOutHash());
590
-
591
- // Fork world state at the block before the first block
592
- const parentBlockNumber = BlockNumber(firstBlock.number - 1);
593
- const fork = await this.worldState.fork(parentBlockNumber);
594
-
595
- try {
596
- // Create checkpoint builder with all existing blocks
597
- const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
598
- checkpointNumber,
599
- constants,
600
- l1ToL2Messages,
601
- previousCheckpointOutHashes,
602
- fork,
603
- blocks,
604
- this.log.getBindings(),
585
+ // Check if incoming slot is strictly greater than last attested
586
+ if (this.lastAttestedProposal && slotNumber <= this.lastAttestedProposal.slotNumber) {
587
+ this.log.warn(
588
+ `Refusing to process a proposal for slot ${slotNumber} given we already attested to a proposal for slot ${this.lastAttestedProposal.slotNumber}`,
605
589
  );
606
-
607
- // Complete the checkpoint to get computed values
608
- const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
609
-
610
- // Compare checkpoint header with proposal
611
- if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
612
- this.log.warn(`Checkpoint header mismatch`, {
613
- ...proposalInfo,
614
- computed: computedCheckpoint.header.toInspect(),
615
- proposal: proposal.checkpointHeader.toInspect(),
616
- });
617
- return { isValid: false, reason: 'checkpoint_header_mismatch' };
618
- }
619
-
620
- // Compare archive root with proposal
621
- if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
622
- this.log.warn(`Archive root mismatch`, {
623
- ...proposalInfo,
624
- computed: computedCheckpoint.archive.root.toString(),
625
- proposal: proposal.archive.toString(),
626
- });
627
- return { isValid: false, reason: 'archive_mismatch' };
628
- }
629
-
630
- // Check that the accumulated epoch out hash matches the value in the proposal.
631
- // The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
632
- const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
633
- const computedEpochOutHash = accumulateCheckpointOutHashes([...previousCheckpointOutHashes, checkpointOutHash]);
634
- const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
635
- if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
636
- this.log.warn(`Epoch out hash mismatch`, {
637
- proposalEpochOutHash: proposalEpochOutHash.toString(),
638
- computedEpochOutHash: computedEpochOutHash.toString(),
639
- checkpointOutHash: checkpointOutHash.toString(),
640
- previousCheckpointOutHashes: previousCheckpointOutHashes.map(h => h.toString()),
641
- ...proposalInfo,
642
- });
643
- return { isValid: false, reason: 'out_hash_mismatch' };
644
- }
645
-
646
- this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
647
- return { isValid: true };
648
- } finally {
649
- await fork.close();
590
+ return false;
650
591
  }
651
- }
652
592
 
653
- /**
654
- * Extract checkpoint global variables from a block.
655
- */
656
- private extractCheckpointConstants(block: L2Block): CheckpointGlobalVariables {
657
- const gv = block.header.globalVariables;
658
- return {
659
- chainId: gv.chainId,
660
- version: gv.version,
661
- slotNumber: gv.slotNumber,
662
- coinbase: gv.coinbase,
663
- feeRecipient: gv.feeRecipient,
664
- gasFees: gv.gasFees,
665
- };
593
+ return true;
666
594
  }
667
595
 
668
- /**
669
- * Uploads blobs for a checkpoint to the filestore (fire and forget).
670
- */
671
- private async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
672
- try {
673
- const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
674
- if (!lastBlockHeader) {
675
- this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
676
- return;
677
- }
596
+ private async createCheckpointAttestationsFromProposal(
597
+ proposal: CheckpointProposalCore,
598
+ attestors: EthAddress[] = [],
599
+ ): Promise<CheckpointAttestation[] | undefined> {
600
+ // Equivocation check: must happen right before signing to minimize the race window
601
+ if (!this.shouldAttestToSlot(proposal.slotNumber)) {
602
+ return undefined;
603
+ }
678
604
 
679
- const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
680
- if (blocks.length === 0) {
681
- this.log.warn(`No blocks found for blob upload`, proposalInfo);
682
- return;
683
- }
605
+ const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
684
606
 
685
- const blobFields = blocks.flatMap(b => b.toBlobFields());
686
- const blobs: Blob[] = getBlobsPerL1Block(blobFields);
687
- await this.blobClient.sendBlobsToFilestore(blobs);
688
- this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
689
- ...proposalInfo,
690
- numBlobs: blobs.length,
691
- });
692
- } catch (err) {
693
- this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
694
- }
607
+ // Track the proposal we attested to (to prevent equivocation)
608
+ this.lastAttestedProposal = proposal;
609
+
610
+ await this.p2pClient.addOwnCheckpointAttestations(attestations);
611
+ return attestations;
695
612
  }
696
613
 
697
614
  private slashInvalidBlock(proposal: BlockProposal) {
@@ -721,6 +638,52 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
721
638
  ]);
722
639
  }
723
640
 
641
+ /**
642
+ * Handle detection of a duplicate proposal (equivocation).
643
+ * Emits a slash event when a proposer sends multiple proposals for the same position.
644
+ */
645
+ private handleDuplicateProposal(info: DuplicateProposalInfo): void {
646
+ const { slot, proposer, type } = info;
647
+
648
+ this.log.warn(`Triggering slash event for duplicate ${type} proposal from ${proposer.toString()} at slot ${slot}`, {
649
+ proposer: proposer.toString(),
650
+ slot,
651
+ type,
652
+ });
653
+
654
+ // Emit slash event
655
+ this.emit(WANT_TO_SLASH_EVENT, [
656
+ {
657
+ validator: proposer,
658
+ amount: this.config.slashDuplicateProposalPenalty,
659
+ offenseType: OffenseType.DUPLICATE_PROPOSAL,
660
+ epochOrSlot: BigInt(slot),
661
+ },
662
+ ]);
663
+ }
664
+
665
+ /**
666
+ * Handle detection of a duplicate attestation (equivocation).
667
+ * Emits a slash event when an attester signs attestations for different proposals at the same slot.
668
+ */
669
+ private handleDuplicateAttestation(info: DuplicateAttestationInfo): void {
670
+ const { slot, attester } = info;
671
+
672
+ this.log.warn(`Triggering slash event for duplicate attestation from ${attester.toString()} at slot ${slot}`, {
673
+ attester: attester.toString(),
674
+ slot,
675
+ });
676
+
677
+ this.emit(WANT_TO_SLASH_EVENT, [
678
+ {
679
+ validator: attester,
680
+ amount: this.config.slashDuplicateAttestationPenalty,
681
+ offenseType: OffenseType.DUPLICATE_ATTESTATION,
682
+ epochOrSlot: BigInt(slot),
683
+ },
684
+ ]);
685
+ }
686
+
724
687
  async createBlockProposal(
725
688
  blockHeader: BlockHeader,
726
689
  indexWithinCheckpoint: IndexWithinCheckpoint,
@@ -730,11 +693,19 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
730
693
  proposerAddress: EthAddress | undefined,
731
694
  options: BlockProposalOptions = {},
732
695
  ): Promise<BlockProposal> {
733
- // TODO(palla/mbps): Prevent double proposals properly
734
- // if (this.previousProposal?.slotNumber === blockHeader.globalVariables.slotNumber) {
735
- // this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
736
- // return Promise.resolve(undefined);
737
- // }
696
+ // Validate that we're not creating a proposal for an older or equal position
697
+ if (this.lastProposedBlock) {
698
+ const lastSlot = this.lastProposedBlock.slotNumber;
699
+ const lastIndex = this.lastProposedBlock.indexWithinCheckpoint;
700
+ const newSlot = blockHeader.globalVariables.slotNumber;
701
+
702
+ if (newSlot < lastSlot || (newSlot === lastSlot && indexWithinCheckpoint <= lastIndex)) {
703
+ throw new Error(
704
+ `Cannot create block proposal for slot ${newSlot} index ${indexWithinCheckpoint}: ` +
705
+ `already proposed block for slot ${lastSlot} index ${lastIndex}`,
706
+ );
707
+ }
708
+ }
738
709
 
739
710
  this.log.info(
740
711
  `Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`,
@@ -751,25 +722,42 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
751
722
  broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal,
752
723
  },
753
724
  );
754
- this.previousProposal = newProposal;
725
+ this.lastProposedBlock = newProposal;
755
726
  return newProposal;
756
727
  }
757
728
 
758
729
  async createCheckpointProposal(
759
730
  checkpointHeader: CheckpointHeader,
760
731
  archive: Fr,
732
+ feeAssetPriceModifier: bigint,
761
733
  lastBlockInfo: CreateCheckpointProposalLastBlockData | undefined,
762
734
  proposerAddress: EthAddress | undefined,
763
735
  options: CheckpointProposalOptions = {},
764
736
  ): Promise<CheckpointProposal> {
737
+ // Validate that we're not creating a proposal for an older or equal slot
738
+ if (this.lastProposedCheckpoint) {
739
+ const lastSlot = this.lastProposedCheckpoint.slotNumber;
740
+ const newSlot = checkpointHeader.slotNumber;
741
+
742
+ if (newSlot <= lastSlot) {
743
+ throw new Error(
744
+ `Cannot create checkpoint proposal for slot ${newSlot}: ` +
745
+ `already proposed checkpoint for slot ${lastSlot}`,
746
+ );
747
+ }
748
+ }
749
+
765
750
  this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
766
- return await this.validationService.createCheckpointProposal(
751
+ const newProposal = await this.validationService.createCheckpointProposal(
767
752
  checkpointHeader,
768
753
  archive,
754
+ feeAssetPriceModifier,
769
755
  lastBlockInfo,
770
756
  proposerAddress,
771
757
  options,
772
758
  );
759
+ this.lastProposedCheckpoint = newProposal;
760
+ return newProposal;
773
761
  }
774
762
 
775
763
  async broadcastBlockProposal(proposal: BlockProposal): Promise<void> {
@@ -791,6 +779,10 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
791
779
  this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, { inCommittee });
792
780
  const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
793
781
 
782
+ if (!attestations) {
783
+ return [];
784
+ }
785
+
794
786
  // We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
795
787
  // other nodes can see that our validators did attest to this block proposal, and do not slash us
796
788
  // due to inactivity for missed attestations.