@aztec/validator-client 0.0.1-commit.7d4e6cd → 0.0.1-commit.858058eac

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 (57) hide show
  1. package/README.md +53 -24
  2. package/dest/block_proposal_handler.d.ts +8 -8
  3. package/dest/block_proposal_handler.d.ts.map +1 -1
  4. package/dest/block_proposal_handler.js +27 -32
  5. package/dest/checkpoint_builder.d.ts +21 -25
  6. package/dest/checkpoint_builder.d.ts.map +1 -1
  7. package/dest/checkpoint_builder.js +50 -32
  8. package/dest/config.d.ts +1 -1
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +12 -14
  11. package/dest/duties/validation_service.d.ts +19 -6
  12. package/dest/duties/validation_service.d.ts.map +1 -1
  13. package/dest/duties/validation_service.js +72 -19
  14. package/dest/factory.d.ts +2 -2
  15. package/dest/factory.d.ts.map +1 -1
  16. package/dest/factory.js +1 -1
  17. package/dest/key_store/ha_key_store.d.ts +99 -0
  18. package/dest/key_store/ha_key_store.d.ts.map +1 -0
  19. package/dest/key_store/ha_key_store.js +208 -0
  20. package/dest/key_store/index.d.ts +2 -1
  21. package/dest/key_store/index.d.ts.map +1 -1
  22. package/dest/key_store/index.js +1 -0
  23. package/dest/key_store/interface.d.ts +36 -6
  24. package/dest/key_store/interface.d.ts.map +1 -1
  25. package/dest/key_store/local_key_store.d.ts +10 -5
  26. package/dest/key_store/local_key_store.d.ts.map +1 -1
  27. package/dest/key_store/local_key_store.js +8 -4
  28. package/dest/key_store/node_keystore_adapter.d.ts +18 -5
  29. package/dest/key_store/node_keystore_adapter.d.ts.map +1 -1
  30. package/dest/key_store/node_keystore_adapter.js +18 -4
  31. package/dest/key_store/web3signer_key_store.d.ts +10 -5
  32. package/dest/key_store/web3signer_key_store.d.ts.map +1 -1
  33. package/dest/key_store/web3signer_key_store.js +8 -4
  34. package/dest/metrics.d.ts +4 -3
  35. package/dest/metrics.d.ts.map +1 -1
  36. package/dest/metrics.js +34 -5
  37. package/dest/tx_validator/tx_validator_factory.d.ts +4 -3
  38. package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -1
  39. package/dest/tx_validator/tx_validator_factory.js +17 -16
  40. package/dest/validator.d.ts +35 -16
  41. package/dest/validator.d.ts.map +1 -1
  42. package/dest/validator.js +194 -91
  43. package/package.json +21 -17
  44. package/src/block_proposal_handler.ts +41 -42
  45. package/src/checkpoint_builder.ts +85 -38
  46. package/src/config.ts +11 -13
  47. package/src/duties/validation_service.ts +91 -23
  48. package/src/factory.ts +1 -0
  49. package/src/key_store/ha_key_store.ts +269 -0
  50. package/src/key_store/index.ts +1 -0
  51. package/src/key_store/interface.ts +44 -5
  52. package/src/key_store/local_key_store.ts +13 -4
  53. package/src/key_store/node_keystore_adapter.ts +27 -4
  54. package/src/key_store/web3signer_key_store.ts +17 -4
  55. package/src/metrics.ts +45 -6
  56. package/src/tx_validator/tx_validator_factory.ts +52 -31
  57. package/src/validator.ts +253 -111
package/src/validator.ts CHANGED
@@ -1,7 +1,13 @@
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 { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
4
+ import {
5
+ BlockNumber,
6
+ CheckpointNumber,
7
+ EpochNumber,
8
+ IndexWithinCheckpoint,
9
+ SlotNumber,
10
+ } from '@aztec/foundation/branded-types';
5
11
  import { Fr } from '@aztec/foundation/curves/bn254';
6
12
  import { TimeoutError } from '@aztec/foundation/error';
7
13
  import type { EthAddress } from '@aztec/foundation/eth-address';
@@ -12,30 +18,34 @@ import { RunningPromise } from '@aztec/foundation/running-promise';
12
18
  import { sleep } from '@aztec/foundation/sleep';
13
19
  import { DateProvider } from '@aztec/foundation/timer';
14
20
  import type { KeystoreManager } from '@aztec/node-keystore';
15
- import type { P2P, PeerId, TxProvider } from '@aztec/p2p';
21
+ import type { DuplicateAttestationInfo, DuplicateProposalInfo, P2P, PeerId } from '@aztec/p2p';
16
22
  import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
17
23
  import { OffenseType, WANT_TO_SLASH_EVENT, type Watcher, type WatcherEmitter } from '@aztec/slasher';
18
24
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
19
- import type { CommitteeAttestationsAndSigners, L2BlockNew, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
25
+ import type { CommitteeAttestationsAndSigners, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
26
+ import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
20
27
  import type {
21
28
  CreateCheckpointProposalLastBlockData,
29
+ ITxProvider,
22
30
  Validator,
23
31
  ValidatorClientFullConfig,
24
32
  WorldStateSynchronizer,
25
33
  } from '@aztec/stdlib/interfaces/server';
26
- import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
27
- import type {
28
- BlockProposal,
29
- BlockProposalOptions,
30
- CheckpointAttestation,
31
- CheckpointProposalCore,
32
- CheckpointProposalOptions,
34
+ import { type L1ToL2MessageSource, accumulateCheckpointOutHashes } from '@aztec/stdlib/messaging';
35
+ import {
36
+ type BlockProposal,
37
+ type BlockProposalOptions,
38
+ type CheckpointAttestation,
39
+ CheckpointProposal,
40
+ type CheckpointProposalCore,
41
+ type CheckpointProposalOptions,
33
42
  } from '@aztec/stdlib/p2p';
34
- import { CheckpointProposal } from '@aztec/stdlib/p2p';
35
43
  import type { CheckpointHeader } from '@aztec/stdlib/rollup';
36
44
  import type { BlockHeader, CheckpointGlobalVariables, Tx } from '@aztec/stdlib/tx';
37
45
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
38
46
  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';
39
49
 
40
50
  import { EventEmitter } from 'events';
41
51
  import type { TypedDataDefinition } from 'viem';
@@ -43,6 +53,8 @@ import type { TypedDataDefinition } from 'viem';
43
53
  import { BlockProposalHandler, type BlockProposalValidationFailureReason } from './block_proposal_handler.js';
44
54
  import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
45
55
  import { ValidationService } from './duties/validation_service.js';
56
+ import { HAKeyStore } from './key_store/ha_key_store.js';
57
+ import type { ExtendedValidatorKeyStore } from './key_store/interface.js';
46
58
  import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
47
59
  import { ValidatorMetrics } from './metrics.js';
48
60
 
@@ -68,21 +80,22 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
68
80
  // Whether it has already registered handlers on the p2p client
69
81
  private hasRegisteredHandlers = false;
70
82
 
71
- // Used to check if we are sending the same proposal twice
72
- private previousProposal?: BlockProposal;
83
+ /** Tracks the last block proposal we created, to detect duplicate proposal attempts. */
84
+ private lastProposedBlock?: BlockProposal;
85
+
86
+ /** Tracks the last checkpoint proposal we created. */
87
+ private lastProposedCheckpoint?: CheckpointProposal;
73
88
 
74
89
  private lastEpochForCommitteeUpdateLoop: EpochNumber | undefined;
75
90
  private epochCacheUpdateLoop: RunningPromise;
76
91
 
77
92
  private proposersOfInvalidBlocks: Set<string> = new Set();
78
93
 
79
- // TODO(palla/mbps): Remove this once checkpoint validation is stable and we can validate all blocks properly.
80
- // Tracks slots for which we have successfully validated a block proposal, so we can attest to checkpoint proposals for those slots.
81
- // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
82
- private validatedBlockSlots: Set<SlotNumber> = new Set();
94
+ /** Tracks the last checkpoint proposal we attested to, to prevent equivocation. */
95
+ private lastAttestedProposal?: CheckpointProposalCore;
83
96
 
84
97
  protected constructor(
85
- private keyStore: NodeKeystoreAdapter,
98
+ private keyStore: ExtendedValidatorKeyStore,
86
99
  private epochCache: EpochCache,
87
100
  private p2pClient: P2P,
88
101
  private blockProposalHandler: BlockProposalHandler,
@@ -165,7 +178,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
165
178
  }
166
179
  }
167
180
 
168
- static new(
181
+ static async new(
169
182
  config: ValidatorClientFullConfig,
170
183
  checkpointsBuilder: FullNodeCheckpointsBuilder,
171
184
  worldState: WorldStateSynchronizer,
@@ -173,7 +186,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
173
186
  p2pClient: P2P,
174
187
  blockSource: L2BlockSource & L2BlockSink,
175
188
  l1ToL2MessageSource: L1ToL2MessageSource,
176
- txProvider: TxProvider,
189
+ txProvider: ITxProvider,
177
190
  keyStoreManager: KeystoreManager,
178
191
  blobClient: BlobClientInterface,
179
192
  dateProvider: DateProvider = new DateProvider(),
@@ -190,14 +203,26 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
190
203
  l1ToL2MessageSource,
191
204
  txProvider,
192
205
  blockProposalValidator,
206
+ epochCache,
193
207
  config,
194
208
  metrics,
195
209
  dateProvider,
196
210
  telemetry,
197
211
  );
198
212
 
213
+ let validatorKeyStore: ExtendedValidatorKeyStore = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
214
+ if (config.haSigningEnabled) {
215
+ // If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
216
+ const haConfig = {
217
+ ...config,
218
+ maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000,
219
+ };
220
+ const { signer } = await createHASigner(haConfig);
221
+ validatorKeyStore = new HAKeyStore(validatorKeyStore, signer);
222
+ }
223
+
199
224
  const validator = new ValidatorClient(
200
- NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager),
225
+ validatorKeyStore,
201
226
  epochCache,
202
227
  p2pClient,
203
228
  blockProposalHandler,
@@ -224,8 +249,8 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
224
249
  return this.blockProposalHandler;
225
250
  }
226
251
 
227
- public signWithAddress(addr: EthAddress, msg: TypedDataDefinition) {
228
- return this.keyStore.signTypedDataWithAddress(addr, msg);
252
+ public signWithAddress(addr: EthAddress, msg: TypedDataDefinition, context: SigningContext) {
253
+ return this.keyStore.signTypedDataWithAddress(addr, msg, context);
229
254
  }
230
255
 
231
256
  public getCoinbaseForAttestor(attestor: EthAddress): EthAddress {
@@ -250,6 +275,8 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
250
275
  return;
251
276
  }
252
277
 
278
+ await this.keyStore.start();
279
+
253
280
  await this.registerHandlers();
254
281
 
255
282
  const myAddresses = this.getValidatorAddresses();
@@ -265,6 +292,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
265
292
 
266
293
  public async stop() {
267
294
  await this.epochCacheUpdateLoop.stop();
295
+ await this.keyStore.stop();
268
296
  }
269
297
 
270
298
  /** Register handlers on the p2p client */
@@ -287,6 +315,16 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
287
315
  ): Promise<CheckpointAttestation[] | undefined> => this.attestToCheckpointProposal(checkpoint, proposalSender);
288
316
  this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
289
317
 
318
+ // Duplicate proposal handler - triggers slashing for equivocation
319
+ this.p2pClient.registerDuplicateProposalCallback((info: DuplicateProposalInfo) => {
320
+ this.handleDuplicateProposal(info);
321
+ });
322
+
323
+ // Duplicate attestation handler - triggers slashing for attestation equivocation
324
+ this.p2pClient.registerDuplicateAttestationCallback((info: DuplicateAttestationInfo) => {
325
+ this.handleDuplicateAttestation(info);
326
+ });
327
+
290
328
  const myAddresses = this.getValidatorAddresses();
291
329
  this.p2pClient.registerThisValidatorAddresses(myAddresses);
292
330
 
@@ -301,6 +339,11 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
301
339
  */
302
340
  async validateBlockProposal(proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> {
303
341
  const slotNumber = proposal.slotNumber;
342
+
343
+ // Note: During escape hatch, we still want to "validate" proposals for observability,
344
+ // but we intentionally reject them and disable slashing invalid block and attestation flow.
345
+ const escapeHatchOpen = await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber);
346
+
304
347
  const proposer = proposal.getSender();
305
348
 
306
349
  // Reject proposals with invalid signatures
@@ -309,6 +352,15 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
309
352
  return false;
310
353
  }
311
354
 
355
+ // Ignore proposals from ourselves (may happen in HA setups)
356
+ if (this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
357
+ this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
358
+ proposer: proposer.toString(),
359
+ slotNumber,
360
+ });
361
+ return false;
362
+ }
363
+
312
364
  // Check if we're in the committee (for metrics purposes)
313
365
  const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
314
366
  const partOfCommittee = inCommittee.length > 0;
@@ -334,7 +386,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
334
386
  const validationResult = await this.blockProposalHandler.handleBlockProposal(
335
387
  proposal,
336
388
  proposalSender,
337
- !!shouldReexecute,
389
+ !!shouldReexecute && !escapeHatchOpen,
338
390
  );
339
391
 
340
392
  if (!validationResult.isValid) {
@@ -359,6 +411,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
359
411
 
360
412
  // Slash invalid block proposals (can happen even when not in committee)
361
413
  if (
414
+ !escapeHatchOpen &&
362
415
  validationResult.reason &&
363
416
  SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) &&
364
417
  slashBroadcastedInvalidBlockPenalty > 0n
@@ -373,11 +426,13 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
373
426
  ...proposalInfo,
374
427
  inCommittee: partOfCommittee,
375
428
  fishermanMode: this.config.fishermanMode || false,
429
+ escapeHatchOpen,
376
430
  });
377
431
 
378
- // TODO(palla/mbps): Remove this once checkpoint validation is stable.
379
- // Track that we successfully validated a block for this slot, so we can attest to checkpoint proposals for it.
380
- this.validatedBlockSlots.add(slotNumber);
432
+ if (escapeHatchOpen) {
433
+ this.log.warn(`Escape hatch open for slot ${slotNumber}, rejecting block proposal`, proposalInfo);
434
+ return false;
435
+ }
381
436
 
382
437
  return true;
383
438
  }
@@ -395,12 +450,27 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
395
450
  const slotNumber = proposal.slotNumber;
396
451
  const proposer = proposal.getSender();
397
452
 
453
+ // 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
+
398
459
  // Reject proposals with invalid signatures
399
460
  if (!proposer) {
400
461
  this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
401
462
  return undefined;
402
463
  }
403
464
 
465
+ // 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}`, {
468
+ proposer: proposer.toString(),
469
+ slotNumber,
470
+ });
471
+ return undefined;
472
+ }
473
+
404
474
  // Check that I have any address in current committee before attesting
405
475
  const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
406
476
  const partOfCommittee = inCommittee.length > 0;
@@ -417,17 +487,9 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
417
487
  fishermanMode: this.config.fishermanMode || false,
418
488
  });
419
489
 
420
- // TODO(palla/mbps): Remove this once checkpoint validation is stable.
421
- // Check that we have successfully validated a block for this slot before attesting to the checkpoint.
422
- if (!this.validatedBlockSlots.has(slotNumber)) {
423
- this.log.warn(`No validated block found for slot ${slotNumber}, refusing to attest to checkpoint`, proposalInfo);
424
- return undefined;
425
- }
426
-
427
490
  // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
428
- // TODO(palla/mbps): Change default to false once checkpoint validation is stable.
429
- if (this.config.skipCheckpointProposalValidation !== false) {
430
- this.log.verbose(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
491
+ if (this.config.skipCheckpointProposalValidation) {
492
+ this.log.warn(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
431
493
  } else {
432
494
  const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
433
495
  if (!validationResult.isValid) {
@@ -482,15 +544,45 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
482
544
  return undefined;
483
545
  }
484
546
 
485
- return this.createCheckpointAttestationsFromProposal(proposal, attestors);
547
+ return await this.createCheckpointAttestationsFromProposal(proposal, attestors);
548
+ }
549
+
550
+ /**
551
+ * Checks if we should attest to a slot based on equivocation prevention rules.
552
+ * @returns true if we should attest, false if we should skip
553
+ */
554
+ private shouldAttestToSlot(slotNumber: SlotNumber): boolean {
555
+ // If attestToEquivocatedProposals is true, always allow
556
+ if (this.config.attestToEquivocatedProposals) {
557
+ return true;
558
+ }
559
+
560
+ // Check if incoming slot is strictly greater than last attested
561
+ if (this.lastAttestedProposal && slotNumber <= this.lastAttestedProposal.slotNumber) {
562
+ this.log.warn(
563
+ `Refusing to process a proposal for slot ${slotNumber} given we already attested to a proposal for slot ${this.lastAttestedProposal.slotNumber}`,
564
+ );
565
+ return false;
566
+ }
567
+
568
+ return true;
486
569
  }
487
570
 
488
571
  private async createCheckpointAttestationsFromProposal(
489
572
  proposal: CheckpointProposalCore,
490
573
  attestors: EthAddress[] = [],
491
- ): Promise<CheckpointAttestation[]> {
574
+ ): Promise<CheckpointAttestation[] | undefined> {
575
+ // Equivocation check: must happen right before signing to minimize the race window
576
+ if (!this.shouldAttestToSlot(proposal.slotNumber)) {
577
+ return undefined;
578
+ }
579
+
492
580
  const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
493
- await this.p2pClient.addCheckpointAttestations(attestations);
581
+
582
+ // Track the proposal we attested to (to prevent equivocation)
583
+ this.lastAttestedProposal = proposal;
584
+
585
+ await this.p2pClient.addOwnCheckpointAttestations(attestations);
494
586
  return attestations;
495
587
  }
496
588
 
@@ -503,7 +595,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
503
595
  proposalInfo: LogData,
504
596
  ): Promise<{ isValid: true } | { isValid: false; reason: string }> {
505
597
  const slot = proposal.slotNumber;
506
- const timeoutSeconds = 10;
598
+ const timeoutSeconds = 10; // TODO(palla/mbps): This should map to the timetable settings
507
599
 
508
600
  // Wait for last block to sync by archive
509
601
  let lastBlockHeader: BlockHeader | undefined;
@@ -531,16 +623,8 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
531
623
  return { isValid: false, reason: 'last_block_not_found' };
532
624
  }
533
625
 
534
- // Get the last full block to determine checkpoint number
535
- const lastBlock = await this.blockSource.getL2BlockNew(lastBlockHeader.getBlockNumber());
536
- if (!lastBlock) {
537
- this.log.warn(`Last block ${lastBlockHeader.getBlockNumber()} not found`, proposalInfo);
538
- return { isValid: false, reason: 'last_block_not_found' };
539
- }
540
- const checkpointNumber = lastBlock.checkpointNumber;
541
-
542
626
  // Get all full blocks for the slot and checkpoint
543
- const blocks = await this.getBlocksForSlot(slot, lastBlockHeader, checkpointNumber);
627
+ const blocks = await this.blockSource.getBlocksForSlot(slot);
544
628
  if (blocks.length === 0) {
545
629
  this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
546
630
  return { isValid: false, reason: 'no_blocks_for_slot' };
@@ -554,10 +638,20 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
554
638
  // Get checkpoint constants from first block
555
639
  const firstBlock = blocks[0];
556
640
  const constants = this.extractCheckpointConstants(firstBlock);
641
+ const checkpointNumber = firstBlock.checkpointNumber;
557
642
 
558
643
  // Get L1-to-L2 messages for this checkpoint
559
644
  const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
560
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
+
561
655
  // Fork world state at the block before the first block
562
656
  const parentBlockNumber = BlockNumber(firstBlock.number - 1);
563
657
  const fork = await this.worldState.fork(parentBlockNumber);
@@ -568,8 +662,10 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
568
662
  checkpointNumber,
569
663
  constants,
570
664
  l1ToL2Messages,
665
+ previousCheckpointOutHashes,
571
666
  fork,
572
667
  blocks,
668
+ this.log.getBindings(),
573
669
  );
574
670
 
575
671
  // Complete the checkpoint to get computed values
@@ -595,6 +691,22 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
595
691
  return { isValid: false, reason: 'archive_mismatch' };
596
692
  }
597
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
+
598
710
  this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
599
711
  return { isValid: true };
600
712
  } finally {
@@ -602,50 +714,10 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
602
714
  }
603
715
  }
604
716
 
605
- /**
606
- * Get all full blocks for a given slot and checkpoint by walking backwards from the last block.
607
- * Returns blocks in ascending order (earliest to latest).
608
- * TODO(palla/mbps): Add getL2BlocksForSlot() to L2BlockSource interface for efficiency.
609
- */
610
- private async getBlocksForSlot(
611
- slot: SlotNumber,
612
- lastBlockHeader: BlockHeader,
613
- checkpointNumber: CheckpointNumber,
614
- ): Promise<L2BlockNew[]> {
615
- const blocks: L2BlockNew[] = [];
616
- let currentHeader = lastBlockHeader;
617
- const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
618
-
619
- while (currentHeader.getSlot() === slot) {
620
- const block = await this.blockSource.getL2BlockNew(currentHeader.getBlockNumber());
621
- if (!block) {
622
- this.log.warn(`Block ${currentHeader.getBlockNumber()} not found while getting blocks for slot ${slot}`);
623
- break;
624
- }
625
- if (block.checkpointNumber !== checkpointNumber) {
626
- break;
627
- }
628
- blocks.unshift(block);
629
-
630
- const prevArchive = currentHeader.lastArchive.root;
631
- if (prevArchive.equals(genesisArchiveRoot)) {
632
- break;
633
- }
634
-
635
- const prevHeader = await this.blockSource.getBlockHeaderByArchive(prevArchive);
636
- if (!prevHeader || prevHeader.getSlot() !== slot) {
637
- break;
638
- }
639
- currentHeader = prevHeader;
640
- }
641
-
642
- return blocks;
643
- }
644
-
645
717
  /**
646
718
  * Extract checkpoint global variables from a block.
647
719
  */
648
- private extractCheckpointConstants(block: L2BlockNew): CheckpointGlobalVariables {
720
+ private extractCheckpointConstants(block: L2Block): CheckpointGlobalVariables {
649
721
  const gv = block.header.globalVariables;
650
722
  return {
651
723
  chainId: gv.chainId,
@@ -668,14 +740,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
668
740
  return;
669
741
  }
670
742
 
671
- // Get the last full block to determine checkpoint number
672
- const lastBlock = await this.blockSource.getL2BlockNew(lastBlockHeader.getBlockNumber());
673
- if (!lastBlock) {
674
- this.log.warn(`Failed to get last block for blob upload`, proposalInfo);
675
- return;
676
- }
677
-
678
- const blocks = await this.getBlocksForSlot(proposal.slotNumber, lastBlockHeader, lastBlock.checkpointNumber);
743
+ const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
679
744
  if (blocks.length === 0) {
680
745
  this.log.warn(`No blocks found for blob upload`, proposalInfo);
681
746
  return;
@@ -720,20 +785,74 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
720
785
  ]);
721
786
  }
722
787
 
788
+ /**
789
+ * Handle detection of a duplicate proposal (equivocation).
790
+ * Emits a slash event when a proposer sends multiple proposals for the same position.
791
+ */
792
+ private handleDuplicateProposal(info: DuplicateProposalInfo): void {
793
+ const { slot, proposer, type } = info;
794
+
795
+ this.log.warn(`Triggering slash event for duplicate ${type} proposal from ${proposer.toString()} at slot ${slot}`, {
796
+ proposer: proposer.toString(),
797
+ slot,
798
+ type,
799
+ });
800
+
801
+ // Emit slash event
802
+ this.emit(WANT_TO_SLASH_EVENT, [
803
+ {
804
+ validator: proposer,
805
+ amount: this.config.slashDuplicateProposalPenalty,
806
+ offenseType: OffenseType.DUPLICATE_PROPOSAL,
807
+ epochOrSlot: BigInt(slot),
808
+ },
809
+ ]);
810
+ }
811
+
812
+ /**
813
+ * Handle detection of a duplicate attestation (equivocation).
814
+ * Emits a slash event when an attester signs attestations for different proposals at the same slot.
815
+ */
816
+ private handleDuplicateAttestation(info: DuplicateAttestationInfo): void {
817
+ const { slot, attester } = info;
818
+
819
+ this.log.warn(`Triggering slash event for duplicate attestation from ${attester.toString()} at slot ${slot}`, {
820
+ attester: attester.toString(),
821
+ slot,
822
+ });
823
+
824
+ this.emit(WANT_TO_SLASH_EVENT, [
825
+ {
826
+ validator: attester,
827
+ amount: this.config.slashDuplicateAttestationPenalty,
828
+ offenseType: OffenseType.DUPLICATE_ATTESTATION,
829
+ epochOrSlot: BigInt(slot),
830
+ },
831
+ ]);
832
+ }
833
+
723
834
  async createBlockProposal(
724
835
  blockHeader: BlockHeader,
725
- indexWithinCheckpoint: number,
836
+ indexWithinCheckpoint: IndexWithinCheckpoint,
726
837
  inHash: Fr,
727
838
  archive: Fr,
728
839
  txs: Tx[],
729
840
  proposerAddress: EthAddress | undefined,
730
- options: BlockProposalOptions,
841
+ options: BlockProposalOptions = {},
731
842
  ): Promise<BlockProposal> {
732
- // TODO(palla/mbps): Prevent double proposals properly
733
- // if (this.previousProposal?.slotNumber === blockHeader.globalVariables.slotNumber) {
734
- // this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
735
- // return Promise.resolve(undefined);
736
- // }
843
+ // Validate that we're not creating a proposal for an older or equal position
844
+ if (this.lastProposedBlock) {
845
+ const lastSlot = this.lastProposedBlock.slotNumber;
846
+ const lastIndex = this.lastProposedBlock.indexWithinCheckpoint;
847
+ const newSlot = blockHeader.globalVariables.slotNumber;
848
+
849
+ if (newSlot < lastSlot || (newSlot === lastSlot && indexWithinCheckpoint <= lastIndex)) {
850
+ throw new Error(
851
+ `Cannot create block proposal for slot ${newSlot} index ${indexWithinCheckpoint}: ` +
852
+ `already proposed block for slot ${lastSlot} index ${lastIndex}`,
853
+ );
854
+ }
855
+ }
737
856
 
738
857
  this.log.info(
739
858
  `Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`,
@@ -750,7 +869,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
750
869
  broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal,
751
870
  },
752
871
  );
753
- this.previousProposal = newProposal;
872
+ this.lastProposedBlock = newProposal;
754
873
  return newProposal;
755
874
  }
756
875
 
@@ -759,16 +878,31 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
759
878
  archive: Fr,
760
879
  lastBlockInfo: CreateCheckpointProposalLastBlockData | undefined,
761
880
  proposerAddress: EthAddress | undefined,
762
- options: CheckpointProposalOptions,
881
+ options: CheckpointProposalOptions = {},
763
882
  ): Promise<CheckpointProposal> {
883
+ // Validate that we're not creating a proposal for an older or equal slot
884
+ if (this.lastProposedCheckpoint) {
885
+ const lastSlot = this.lastProposedCheckpoint.slotNumber;
886
+ const newSlot = checkpointHeader.slotNumber;
887
+
888
+ if (newSlot <= lastSlot) {
889
+ throw new Error(
890
+ `Cannot create checkpoint proposal for slot ${newSlot}: ` +
891
+ `already proposed checkpoint for slot ${lastSlot}`,
892
+ );
893
+ }
894
+ }
895
+
764
896
  this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
765
- return await this.validationService.createCheckpointProposal(
897
+ const newProposal = await this.validationService.createCheckpointProposal(
766
898
  checkpointHeader,
767
899
  archive,
768
900
  lastBlockInfo,
769
901
  proposerAddress,
770
902
  options,
771
903
  );
904
+ this.lastProposedCheckpoint = newProposal;
905
+ return newProposal;
772
906
  }
773
907
 
774
908
  async broadcastBlockProposal(proposal: BlockProposal): Promise<void> {
@@ -778,8 +912,10 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
778
912
  async signAttestationsAndSigners(
779
913
  attestationsAndSigners: CommitteeAttestationsAndSigners,
780
914
  proposer: EthAddress,
915
+ slot: SlotNumber,
916
+ blockNumber: BlockNumber | CheckpointNumber,
781
917
  ): Promise<Signature> {
782
- return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
918
+ return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber);
783
919
  }
784
920
 
785
921
  async collectOwnAttestations(proposal: CheckpointProposal): Promise<CheckpointAttestation[]> {
@@ -788,6 +924,10 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
788
924
  this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, { inCommittee });
789
925
  const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
790
926
 
927
+ if (!attestations) {
928
+ return [];
929
+ }
930
+
791
931
  // We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
792
932
  // other nodes can see that our validators did attest to this block proposal, and do not slash us
793
933
  // due to inactivity for missed attestations.
@@ -886,7 +1026,9 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
886
1026
  }
887
1027
 
888
1028
  const payloadToSign = authRequest.getPayloadToSign();
889
- const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign);
1029
+ // AUTH_REQUEST doesn't require HA protection - multiple signatures are safe
1030
+ const context: SigningContext = { dutyType: DutyType.AUTH_REQUEST };
1031
+ const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign, context);
890
1032
  const authResponse = new AuthResponse(statusMessage, signature);
891
1033
  return authResponse.toBuffer();
892
1034
  }