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

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 (69) hide show
  1. package/README.md +95 -24
  2. package/dest/block_proposal_handler.d.ts +10 -10
  3. package/dest/block_proposal_handler.d.ts.map +1 -1
  4. package/dest/block_proposal_handler.js +76 -76
  5. package/dest/checkpoint_builder.d.ts +31 -25
  6. package/dest/checkpoint_builder.d.ts.map +1 -1
  7. package/dest/checkpoint_builder.js +114 -41
  8. package/dest/config.d.ts +1 -1
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +33 -14
  11. package/dest/duties/validation_service.d.ts +20 -7
  12. package/dest/duties/validation_service.d.ts.map +1 -1
  13. package/dest/duties/validation_service.js +69 -22
  14. package/dest/factory.d.ts +2 -2
  15. package/dest/factory.d.ts.map +1 -1
  16. package/dest/factory.js +3 -2
  17. package/dest/index.d.ts +1 -2
  18. package/dest/index.d.ts.map +1 -1
  19. package/dest/index.js +0 -1
  20. package/dest/key_store/ha_key_store.d.ts +99 -0
  21. package/dest/key_store/ha_key_store.d.ts.map +1 -0
  22. package/dest/key_store/ha_key_store.js +208 -0
  23. package/dest/key_store/index.d.ts +2 -1
  24. package/dest/key_store/index.d.ts.map +1 -1
  25. package/dest/key_store/index.js +1 -0
  26. package/dest/key_store/interface.d.ts +36 -6
  27. package/dest/key_store/interface.d.ts.map +1 -1
  28. package/dest/key_store/local_key_store.d.ts +10 -5
  29. package/dest/key_store/local_key_store.d.ts.map +1 -1
  30. package/dest/key_store/local_key_store.js +8 -4
  31. package/dest/key_store/node_keystore_adapter.d.ts +18 -5
  32. package/dest/key_store/node_keystore_adapter.d.ts.map +1 -1
  33. package/dest/key_store/node_keystore_adapter.js +18 -4
  34. package/dest/key_store/web3signer_key_store.d.ts +10 -5
  35. package/dest/key_store/web3signer_key_store.d.ts.map +1 -1
  36. package/dest/key_store/web3signer_key_store.js +8 -4
  37. package/dest/metrics.d.ts +12 -3
  38. package/dest/metrics.d.ts.map +1 -1
  39. package/dest/metrics.js +46 -5
  40. package/dest/validator.d.ts +45 -18
  41. package/dest/validator.d.ts.map +1 -1
  42. package/dest/validator.js +262 -98
  43. package/package.json +21 -17
  44. package/src/block_proposal_handler.ts +93 -95
  45. package/src/checkpoint_builder.ts +171 -48
  46. package/src/config.ts +32 -13
  47. package/src/duties/validation_service.ts +94 -25
  48. package/src/factory.ts +2 -0
  49. package/src/index.ts +0 -1
  50. package/src/key_store/ha_key_store.ts +269 -0
  51. package/src/key_store/index.ts +1 -0
  52. package/src/key_store/interface.ts +44 -5
  53. package/src/key_store/local_key_store.ts +13 -4
  54. package/src/key_store/node_keystore_adapter.ts +27 -4
  55. package/src/key_store/web3signer_key_store.ts +17 -4
  56. package/src/metrics.ts +63 -6
  57. package/src/validator.ts +326 -116
  58. package/dest/tx_validator/index.d.ts +0 -3
  59. package/dest/tx_validator/index.d.ts.map +0 -1
  60. package/dest/tx_validator/index.js +0 -2
  61. package/dest/tx_validator/nullifier_cache.d.ts +0 -14
  62. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  63. package/dest/tx_validator/nullifier_cache.js +0 -24
  64. package/dest/tx_validator/tx_validator_factory.d.ts +0 -18
  65. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  66. package/dest/tx_validator/tx_validator_factory.js +0 -53
  67. package/src/tx_validator/index.ts +0 -2
  68. package/src/tx_validator/nullifier_cache.ts +0 -30
  69. package/src/tx_validator/tx_validator_factory.ts +0 -133
package/src/validator.ts CHANGED
@@ -1,7 +1,14 @@
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 { validateFeeAssetPriceModifier } from '@aztec/ethereum/contracts';
5
+ import {
6
+ BlockNumber,
7
+ CheckpointNumber,
8
+ EpochNumber,
9
+ IndexWithinCheckpoint,
10
+ SlotNumber,
11
+ } from '@aztec/foundation/branded-types';
5
12
  import { Fr } from '@aztec/foundation/curves/bn254';
6
13
  import { TimeoutError } from '@aztec/foundation/error';
7
14
  import type { EthAddress } from '@aztec/foundation/eth-address';
@@ -12,30 +19,36 @@ import { RunningPromise } from '@aztec/foundation/running-promise';
12
19
  import { sleep } from '@aztec/foundation/sleep';
13
20
  import { DateProvider } from '@aztec/foundation/timer';
14
21
  import type { KeystoreManager } from '@aztec/node-keystore';
15
- import type { P2P, PeerId, TxProvider } from '@aztec/p2p';
22
+ import type { DuplicateAttestationInfo, DuplicateProposalInfo, P2P, PeerId } from '@aztec/p2p';
16
23
  import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
17
24
  import { OffenseType, WANT_TO_SLASH_EVENT, type Watcher, type WatcherEmitter } from '@aztec/slasher';
18
25
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
19
- import type { CommitteeAttestationsAndSigners, L2BlockNew, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
26
+ import type { CommitteeAttestationsAndSigners, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
27
+ import { validateCheckpoint } from '@aztec/stdlib/checkpoint';
28
+ import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
20
29
  import type {
21
30
  CreateCheckpointProposalLastBlockData,
31
+ ITxProvider,
22
32
  Validator,
23
33
  ValidatorClientFullConfig,
24
34
  WorldStateSynchronizer,
25
35
  } 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,
36
+ import { type L1ToL2MessageSource, accumulateCheckpointOutHashes } from '@aztec/stdlib/messaging';
37
+ import {
38
+ type BlockProposal,
39
+ type BlockProposalOptions,
40
+ type CheckpointAttestation,
41
+ CheckpointProposal,
42
+ type CheckpointProposalCore,
43
+ type CheckpointProposalOptions,
33
44
  } from '@aztec/stdlib/p2p';
34
- import { CheckpointProposal } from '@aztec/stdlib/p2p';
35
45
  import type { CheckpointHeader } from '@aztec/stdlib/rollup';
36
46
  import type { BlockHeader, CheckpointGlobalVariables, Tx } from '@aztec/stdlib/tx';
37
47
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
38
48
  import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
49
+ import { createHASigner, createLocalSignerWithProtection } from '@aztec/validator-ha-signer/factory';
50
+ import { DutyType, type SigningContext } from '@aztec/validator-ha-signer/types';
51
+ import type { ValidatorHASigner } from '@aztec/validator-ha-signer/validator-ha-signer';
39
52
 
40
53
  import { EventEmitter } from 'events';
41
54
  import type { TypedDataDefinition } from 'viem';
@@ -43,6 +56,8 @@ import type { TypedDataDefinition } from 'viem';
43
56
  import { BlockProposalHandler, type BlockProposalValidationFailureReason } from './block_proposal_handler.js';
44
57
  import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
45
58
  import { ValidationService } from './duties/validation_service.js';
59
+ import { HAKeyStore } from './key_store/ha_key_store.js';
60
+ import type { ExtendedValidatorKeyStore } from './key_store/interface.js';
46
61
  import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
47
62
  import { ValidatorMetrics } from './metrics.js';
48
63
 
@@ -64,25 +79,27 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
64
79
  private validationService: ValidationService;
65
80
  private metrics: ValidatorMetrics;
66
81
  private log: Logger;
67
-
68
82
  // Whether it has already registered handlers on the p2p client
69
83
  private hasRegisteredHandlers = false;
70
84
 
71
- // Used to check if we are sending the same proposal twice
72
- private previousProposal?: BlockProposal;
85
+ /** Tracks the last block proposal we created, to detect duplicate proposal attempts. */
86
+ private lastProposedBlock?: BlockProposal;
87
+
88
+ /** Tracks the last checkpoint proposal we created. */
89
+ private lastProposedCheckpoint?: CheckpointProposal;
73
90
 
74
91
  private lastEpochForCommitteeUpdateLoop: EpochNumber | undefined;
75
92
  private epochCacheUpdateLoop: RunningPromise;
93
+ /** Tracks the last epoch in which each attester successfully submitted at least one attestation. */
94
+ private lastAttestedEpochByAttester: Map<string, EpochNumber> = new Map();
76
95
 
77
96
  private proposersOfInvalidBlocks: Set<string> = new Set();
78
97
 
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();
98
+ /** Tracks the last checkpoint proposal we attested to, to prevent equivocation. */
99
+ private lastAttestedProposal?: CheckpointProposalCore;
83
100
 
84
101
  protected constructor(
85
- private keyStore: NodeKeystoreAdapter,
102
+ private keyStore: ExtendedValidatorKeyStore,
86
103
  private epochCache: EpochCache,
87
104
  private p2pClient: P2P,
88
105
  private blockProposalHandler: BlockProposalHandler,
@@ -92,6 +109,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
92
109
  private l1ToL2MessageSource: L1ToL2MessageSource,
93
110
  private config: ValidatorClientFullConfig,
94
111
  private blobClient: BlobClientInterface,
112
+ private slashingProtectionSigner: ValidatorHASigner,
95
113
  private dateProvider: DateProvider = new DateProvider(),
96
114
  telemetry: TelemetryClient = getTelemetryClient(),
97
115
  log = createLogger('validator'),
@@ -145,6 +163,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
145
163
  this.log.trace(`No committee found for slot`);
146
164
  return;
147
165
  }
166
+ this.metrics.setCurrentEpoch(epoch);
148
167
  if (epoch !== this.lastEpochForCommitteeUpdateLoop) {
149
168
  const me = this.getValidatorAddresses();
150
169
  const committeeSet = new Set(committee.map(v => v.toString()));
@@ -165,7 +184,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
165
184
  }
166
185
  }
167
186
 
168
- static new(
187
+ static async new(
169
188
  config: ValidatorClientFullConfig,
170
189
  checkpointsBuilder: FullNodeCheckpointsBuilder,
171
190
  worldState: WorldStateSynchronizer,
@@ -173,7 +192,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
173
192
  p2pClient: P2P,
174
193
  blockSource: L2BlockSource & L2BlockSink,
175
194
  l1ToL2MessageSource: L1ToL2MessageSource,
176
- txProvider: TxProvider,
195
+ txProvider: ITxProvider,
177
196
  keyStoreManager: KeystoreManager,
178
197
  blobClient: BlobClientInterface,
179
198
  dateProvider: DateProvider = new DateProvider(),
@@ -182,6 +201,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
182
201
  const metrics = new ValidatorMetrics(telemetry);
183
202
  const blockProposalValidator = new BlockProposalValidator(epochCache, {
184
203
  txsPermitted: !config.disableTransactions,
204
+ maxTxsPerBlock: config.validateMaxTxsPerBlock,
185
205
  });
186
206
  const blockProposalHandler = new BlockProposalHandler(
187
207
  checkpointsBuilder,
@@ -190,14 +210,38 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
190
210
  l1ToL2MessageSource,
191
211
  txProvider,
192
212
  blockProposalValidator,
213
+ epochCache,
193
214
  config,
194
215
  metrics,
195
216
  dateProvider,
196
217
  telemetry,
197
218
  );
198
219
 
220
+ const nodeKeystoreAdapter = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
221
+ let slashingProtectionSigner: ValidatorHASigner;
222
+ if (config.haSigningEnabled) {
223
+ // Multi-node HA mode: use PostgreSQL-backed distributed locking.
224
+ // If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
225
+ const haConfig = {
226
+ ...config,
227
+ maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000,
228
+ };
229
+ ({ signer: slashingProtectionSigner } = await createHASigner(haConfig, {
230
+ telemetryClient: telemetry,
231
+ dateProvider,
232
+ }));
233
+ } else {
234
+ // Single-node mode: use LMDB-backed local signing protection.
235
+ // This prevents double-signing if the node crashes and restarts mid-proposal.
236
+ ({ signer: slashingProtectionSigner } = await createLocalSignerWithProtection(config, {
237
+ telemetryClient: telemetry,
238
+ dateProvider,
239
+ }));
240
+ }
241
+ const validatorKeyStore: ExtendedValidatorKeyStore = new HAKeyStore(nodeKeystoreAdapter, slashingProtectionSigner);
242
+
199
243
  const validator = new ValidatorClient(
200
- NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager),
244
+ validatorKeyStore,
201
245
  epochCache,
202
246
  p2pClient,
203
247
  blockProposalHandler,
@@ -207,6 +251,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
207
251
  l1ToL2MessageSource,
208
252
  config,
209
253
  blobClient,
254
+ slashingProtectionSigner,
210
255
  dateProvider,
211
256
  telemetry,
212
257
  );
@@ -224,8 +269,8 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
224
269
  return this.blockProposalHandler;
225
270
  }
226
271
 
227
- public signWithAddress(addr: EthAddress, msg: TypedDataDefinition) {
228
- return this.keyStore.signTypedDataWithAddress(addr, msg);
272
+ public signWithAddress(addr: EthAddress, msg: TypedDataDefinition, context: SigningContext) {
273
+ return this.keyStore.signTypedDataWithAddress(addr, msg, context);
229
274
  }
230
275
 
231
276
  public getCoinbaseForAttestor(attestor: EthAddress): EthAddress {
@@ -244,12 +289,20 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
244
289
  this.config = { ...this.config, ...config };
245
290
  }
246
291
 
292
+ public reloadKeystore(newManager: KeystoreManager): void {
293
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
294
+ this.keyStore = new HAKeyStore(newAdapter, this.slashingProtectionSigner);
295
+ this.validationService = new ValidationService(this.keyStore, this.log.createChild('validation-service'));
296
+ }
297
+
247
298
  public async start() {
248
299
  if (this.epochCacheUpdateLoop.isRunning()) {
249
300
  this.log.warn(`Validator client already started`);
250
301
  return;
251
302
  }
252
303
 
304
+ await this.keyStore.start();
305
+
253
306
  await this.registerHandlers();
254
307
 
255
308
  const myAddresses = this.getValidatorAddresses();
@@ -265,6 +318,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
265
318
 
266
319
  public async stop() {
267
320
  await this.epochCacheUpdateLoop.stop();
321
+ await this.keyStore.stop();
268
322
  }
269
323
 
270
324
  /** Register handlers on the p2p client */
@@ -287,6 +341,16 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
287
341
  ): Promise<CheckpointAttestation[] | undefined> => this.attestToCheckpointProposal(checkpoint, proposalSender);
288
342
  this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
289
343
 
344
+ // Duplicate proposal handler - triggers slashing for equivocation
345
+ this.p2pClient.registerDuplicateProposalCallback((info: DuplicateProposalInfo) => {
346
+ this.handleDuplicateProposal(info);
347
+ });
348
+
349
+ // Duplicate attestation handler - triggers slashing for attestation equivocation
350
+ this.p2pClient.registerDuplicateAttestationCallback((info: DuplicateAttestationInfo) => {
351
+ this.handleDuplicateAttestation(info);
352
+ });
353
+
290
354
  const myAddresses = this.getValidatorAddresses();
291
355
  this.p2pClient.registerThisValidatorAddresses(myAddresses);
292
356
 
@@ -301,6 +365,11 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
301
365
  */
302
366
  async validateBlockProposal(proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> {
303
367
  const slotNumber = proposal.slotNumber;
368
+
369
+ // Note: During escape hatch, we still want to "validate" proposals for observability,
370
+ // but we intentionally reject them and disable slashing invalid block and attestation flow.
371
+ const escapeHatchOpen = await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber);
372
+
304
373
  const proposer = proposal.getSender();
305
374
 
306
375
  // Reject proposals with invalid signatures
@@ -309,6 +378,15 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
309
378
  return false;
310
379
  }
311
380
 
381
+ // Ignore proposals from ourselves (may happen in HA setups)
382
+ if (this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
383
+ this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
384
+ proposer: proposer.toString(),
385
+ slotNumber,
386
+ });
387
+ return false;
388
+ }
389
+
312
390
  // Check if we're in the committee (for metrics purposes)
313
391
  const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
314
392
  const partOfCommittee = inCommittee.length > 0;
@@ -334,7 +412,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
334
412
  const validationResult = await this.blockProposalHandler.handleBlockProposal(
335
413
  proposal,
336
414
  proposalSender,
337
- !!shouldReexecute,
415
+ !!shouldReexecute && !escapeHatchOpen,
338
416
  );
339
417
 
340
418
  if (!validationResult.isValid) {
@@ -359,6 +437,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
359
437
 
360
438
  // Slash invalid block proposals (can happen even when not in committee)
361
439
  if (
440
+ !escapeHatchOpen &&
362
441
  validationResult.reason &&
363
442
  SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) &&
364
443
  slashBroadcastedInvalidBlockPenalty > 0n
@@ -373,11 +452,13 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
373
452
  ...proposalInfo,
374
453
  inCommittee: partOfCommittee,
375
454
  fishermanMode: this.config.fishermanMode || false,
455
+ escapeHatchOpen,
376
456
  });
377
457
 
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);
458
+ if (escapeHatchOpen) {
459
+ this.log.warn(`Escape hatch open for slot ${slotNumber}, rejecting block proposal`, proposalInfo);
460
+ return false;
461
+ }
381
462
 
382
463
  return true;
383
464
  }
@@ -395,12 +476,35 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
395
476
  const slotNumber = proposal.slotNumber;
396
477
  const proposer = proposal.getSender();
397
478
 
479
+ // If escape hatch is open for this slot's epoch, do not attest.
480
+ if (await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber)) {
481
+ this.log.warn(`Escape hatch open for slot ${slotNumber}, skipping checkpoint attestation handling`);
482
+ return undefined;
483
+ }
484
+
398
485
  // Reject proposals with invalid signatures
399
486
  if (!proposer) {
400
487
  this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
401
488
  return undefined;
402
489
  }
403
490
 
491
+ // Ignore proposals from ourselves (may happen in HA setups)
492
+ if (this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
493
+ this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
494
+ proposer: proposer.toString(),
495
+ slotNumber,
496
+ });
497
+ return undefined;
498
+ }
499
+
500
+ // Validate fee asset price modifier is within allowed range
501
+ if (!validateFeeAssetPriceModifier(proposal.feeAssetPriceModifier)) {
502
+ this.log.warn(
503
+ `Received checkpoint proposal with invalid feeAssetPriceModifier ${proposal.feeAssetPriceModifier} for slot ${slotNumber}`,
504
+ );
505
+ return undefined;
506
+ }
507
+
404
508
  // Check that I have any address in current committee before attesting
405
509
  const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
406
510
  const partOfCommittee = inCommittee.length > 0;
@@ -409,25 +513,15 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
409
513
  slotNumber,
410
514
  archive: proposal.archive.toString(),
411
515
  proposer: proposer.toString(),
412
- txCount: proposal.txHashes.length,
413
516
  };
414
517
  this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
415
518
  ...proposalInfo,
416
- txHashes: proposal.txHashes.map(t => t.toString()),
417
519
  fishermanMode: this.config.fishermanMode || false,
418
520
  });
419
521
 
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
522
  // 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);
523
+ if (this.config.skipCheckpointProposalValidation) {
524
+ this.log.warn(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
431
525
  } else {
432
526
  const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
433
527
  if (!validationResult.isValid) {
@@ -457,6 +551,17 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
457
551
 
458
552
  this.metrics.incSuccessfulAttestations(inCommittee.length);
459
553
 
554
+ // Track epoch participation per attester: count each (attester, epoch) pair at most once
555
+ const proposalEpoch = getEpochAtSlot(slotNumber, this.epochCache.getL1Constants());
556
+ for (const attester of inCommittee) {
557
+ const key = attester.toString();
558
+ const lastEpoch = this.lastAttestedEpochByAttester.get(key);
559
+ if (lastEpoch === undefined || proposalEpoch > lastEpoch) {
560
+ this.lastAttestedEpochByAttester.set(key, proposalEpoch);
561
+ this.metrics.incAttestedEpochCount(attester);
562
+ }
563
+ }
564
+
460
565
  // Determine which validators should attest
461
566
  let attestors: EthAddress[];
462
567
  if (partOfCommittee) {
@@ -482,15 +587,45 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
482
587
  return undefined;
483
588
  }
484
589
 
485
- return this.createCheckpointAttestationsFromProposal(proposal, attestors);
590
+ return await this.createCheckpointAttestationsFromProposal(proposal, attestors);
591
+ }
592
+
593
+ /**
594
+ * Checks if we should attest to a slot based on equivocation prevention rules.
595
+ * @returns true if we should attest, false if we should skip
596
+ */
597
+ private shouldAttestToSlot(slotNumber: SlotNumber): boolean {
598
+ // If attestToEquivocatedProposals is true, always allow
599
+ if (this.config.attestToEquivocatedProposals) {
600
+ return true;
601
+ }
602
+
603
+ // Check if incoming slot is strictly greater than last attested
604
+ if (this.lastAttestedProposal && slotNumber <= this.lastAttestedProposal.slotNumber) {
605
+ this.log.warn(
606
+ `Refusing to process a proposal for slot ${slotNumber} given we already attested to a proposal for slot ${this.lastAttestedProposal.slotNumber}`,
607
+ );
608
+ return false;
609
+ }
610
+
611
+ return true;
486
612
  }
487
613
 
488
614
  private async createCheckpointAttestationsFromProposal(
489
615
  proposal: CheckpointProposalCore,
490
616
  attestors: EthAddress[] = [],
491
- ): Promise<CheckpointAttestation[]> {
617
+ ): Promise<CheckpointAttestation[] | undefined> {
618
+ // Equivocation check: must happen right before signing to minimize the race window
619
+ if (!this.shouldAttestToSlot(proposal.slotNumber)) {
620
+ return undefined;
621
+ }
622
+
492
623
  const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
493
- await this.p2pClient.addCheckpointAttestations(attestations);
624
+
625
+ // Track the proposal we attested to (to prevent equivocation)
626
+ this.lastAttestedProposal = proposal;
627
+
628
+ await this.p2pClient.addOwnCheckpointAttestations(attestations);
494
629
  return attestations;
495
630
  }
496
631
 
@@ -503,7 +638,11 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
503
638
  proposalInfo: LogData,
504
639
  ): Promise<{ isValid: true } | { isValid: false; reason: string }> {
505
640
  const slot = proposal.slotNumber;
506
- const timeoutSeconds = 10;
641
+
642
+ // Timeout block syncing at the start of the next slot
643
+ const config = this.checkpointsBuilder.getConfig();
644
+ const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
645
+ const timeoutSeconds = Math.max(1, nextSlotTimestampSeconds - Math.floor(this.dateProvider.now() / 1000));
507
646
 
508
647
  // Wait for last block to sync by archive
509
648
  let lastBlockHeader: BlockHeader | undefined;
@@ -531,21 +670,19 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
531
670
  return { isValid: false, reason: 'last_block_not_found' };
532
671
  }
533
672
 
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
673
  // Get all full blocks for the slot and checkpoint
543
- const blocks = await this.getBlocksForSlot(slot, lastBlockHeader, checkpointNumber);
674
+ const blocks = await this.blockSource.getBlocksForSlot(slot);
544
675
  if (blocks.length === 0) {
545
676
  this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
546
677
  return { isValid: false, reason: 'no_blocks_for_slot' };
547
678
  }
548
679
 
680
+ // Ensure the last block for this slot matches the archive in the checkpoint proposal
681
+ if (!blocks.at(-1)?.archive.root.equals(proposal.archive)) {
682
+ this.log.warn(`Last block archive mismatch for checkpoint proposal`, proposalInfo);
683
+ return { isValid: false, reason: 'last_block_archive_mismatch' };
684
+ }
685
+
549
686
  this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
550
687
  ...proposalInfo,
551
688
  blockNumbers: blocks.map(b => b.number),
@@ -554,10 +691,17 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
554
691
  // Get checkpoint constants from first block
555
692
  const firstBlock = blocks[0];
556
693
  const constants = this.extractCheckpointConstants(firstBlock);
694
+ const checkpointNumber = firstBlock.checkpointNumber;
557
695
 
558
696
  // Get L1-to-L2 messages for this checkpoint
559
697
  const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
560
698
 
699
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
700
+ const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
701
+ const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
702
+ .filter(c => c.checkpointNumber < checkpointNumber)
703
+ .map(c => c.checkpointOutHash);
704
+
561
705
  // Fork world state at the block before the first block
562
706
  const parentBlockNumber = BlockNumber(firstBlock.number - 1);
563
707
  const fork = await this.worldState.fork(parentBlockNumber);
@@ -567,9 +711,12 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
567
711
  const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
568
712
  checkpointNumber,
569
713
  constants,
714
+ proposal.feeAssetPriceModifier,
570
715
  l1ToL2Messages,
716
+ previousCheckpointOutHashes,
571
717
  fork,
572
718
  blocks,
719
+ this.log.getBindings(),
573
720
  );
574
721
 
575
722
  // Complete the checkpoint to get computed values
@@ -595,62 +742,53 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
595
742
  return { isValid: false, reason: 'archive_mismatch' };
596
743
  }
597
744
 
598
- this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
599
- return { isValid: true };
600
- } finally {
601
- await fork.close();
602
- }
603
- }
604
-
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;
745
+ // Check that the accumulated epoch out hash matches the value in the proposal.
746
+ // The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
747
+ const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
748
+ const computedEpochOutHash = accumulateCheckpointOutHashes([...previousCheckpointOutHashes, checkpointOutHash]);
749
+ const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
750
+ if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
751
+ this.log.warn(`Epoch out hash mismatch`, {
752
+ proposalEpochOutHash: proposalEpochOutHash.toString(),
753
+ computedEpochOutHash: computedEpochOutHash.toString(),
754
+ checkpointOutHash: checkpointOutHash.toString(),
755
+ previousCheckpointOutHashes: previousCheckpointOutHashes.map(h => h.toString()),
756
+ ...proposalInfo,
757
+ });
758
+ return { isValid: false, reason: 'out_hash_mismatch' };
627
759
  }
628
- blocks.unshift(block);
629
760
 
630
- const prevArchive = currentHeader.lastArchive.root;
631
- if (prevArchive.equals(genesisArchiveRoot)) {
632
- break;
761
+ // Final round of validations on the checkpoint, just in case.
762
+ try {
763
+ validateCheckpoint(computedCheckpoint, {
764
+ rollupManaLimit: this.checkpointsBuilder.getConfig().rollupManaLimit,
765
+ maxDABlockGas: this.config.validateMaxDABlockGas,
766
+ maxL2BlockGas: this.config.validateMaxL2BlockGas,
767
+ maxTxsPerBlock: this.config.validateMaxTxsPerBlock,
768
+ maxTxsPerCheckpoint: this.config.validateMaxTxsPerCheckpoint,
769
+ });
770
+ } catch (err) {
771
+ this.log.warn(`Checkpoint validation failed: ${err}`, proposalInfo);
772
+ return { isValid: false, reason: 'checkpoint_validation_failed' };
633
773
  }
634
774
 
635
- const prevHeader = await this.blockSource.getBlockHeaderByArchive(prevArchive);
636
- if (!prevHeader || prevHeader.getSlot() !== slot) {
637
- break;
638
- }
639
- currentHeader = prevHeader;
775
+ this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
776
+ return { isValid: true };
777
+ } finally {
778
+ await fork.close();
640
779
  }
641
-
642
- return blocks;
643
780
  }
644
781
 
645
782
  /**
646
783
  * Extract checkpoint global variables from a block.
647
784
  */
648
- private extractCheckpointConstants(block: L2BlockNew): CheckpointGlobalVariables {
785
+ private extractCheckpointConstants(block: L2Block): CheckpointGlobalVariables {
649
786
  const gv = block.header.globalVariables;
650
787
  return {
651
788
  chainId: gv.chainId,
652
789
  version: gv.version,
653
790
  slotNumber: gv.slotNumber,
791
+ timestamp: gv.timestamp,
654
792
  coinbase: gv.coinbase,
655
793
  feeRecipient: gv.feeRecipient,
656
794
  gasFees: gv.gasFees,
@@ -660,7 +798,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
660
798
  /**
661
799
  * Uploads blobs for a checkpoint to the filestore (fire and forget).
662
800
  */
663
- private async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
801
+ protected async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
664
802
  try {
665
803
  const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
666
804
  if (!lastBlockHeader) {
@@ -668,21 +806,14 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
668
806
  return;
669
807
  }
670
808
 
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);
809
+ const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
679
810
  if (blocks.length === 0) {
680
811
  this.log.warn(`No blocks found for blob upload`, proposalInfo);
681
812
  return;
682
813
  }
683
814
 
684
815
  const blobFields = blocks.flatMap(b => b.toBlobFields());
685
- const blobs: Blob[] = getBlobsPerL1Block(blobFields);
816
+ const blobs: Blob[] = await getBlobsPerL1Block(blobFields);
686
817
  await this.blobClient.sendBlobsToFilestore(blobs);
687
818
  this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
688
819
  ...proposalInfo,
@@ -720,20 +851,74 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
720
851
  ]);
721
852
  }
722
853
 
854
+ /**
855
+ * Handle detection of a duplicate proposal (equivocation).
856
+ * Emits a slash event when a proposer sends multiple proposals for the same position.
857
+ */
858
+ private handleDuplicateProposal(info: DuplicateProposalInfo): void {
859
+ const { slot, proposer, type } = info;
860
+
861
+ this.log.warn(`Triggering slash event for duplicate ${type} proposal from ${proposer.toString()} at slot ${slot}`, {
862
+ proposer: proposer.toString(),
863
+ slot,
864
+ type,
865
+ });
866
+
867
+ // Emit slash event
868
+ this.emit(WANT_TO_SLASH_EVENT, [
869
+ {
870
+ validator: proposer,
871
+ amount: this.config.slashDuplicateProposalPenalty,
872
+ offenseType: OffenseType.DUPLICATE_PROPOSAL,
873
+ epochOrSlot: BigInt(slot),
874
+ },
875
+ ]);
876
+ }
877
+
878
+ /**
879
+ * Handle detection of a duplicate attestation (equivocation).
880
+ * Emits a slash event when an attester signs attestations for different proposals at the same slot.
881
+ */
882
+ private handleDuplicateAttestation(info: DuplicateAttestationInfo): void {
883
+ const { slot, attester } = info;
884
+
885
+ this.log.warn(`Triggering slash event for duplicate attestation from ${attester.toString()} at slot ${slot}`, {
886
+ attester: attester.toString(),
887
+ slot,
888
+ });
889
+
890
+ this.emit(WANT_TO_SLASH_EVENT, [
891
+ {
892
+ validator: attester,
893
+ amount: this.config.slashDuplicateAttestationPenalty,
894
+ offenseType: OffenseType.DUPLICATE_ATTESTATION,
895
+ epochOrSlot: BigInt(slot),
896
+ },
897
+ ]);
898
+ }
899
+
723
900
  async createBlockProposal(
724
901
  blockHeader: BlockHeader,
725
- indexWithinCheckpoint: number,
902
+ indexWithinCheckpoint: IndexWithinCheckpoint,
726
903
  inHash: Fr,
727
904
  archive: Fr,
728
905
  txs: Tx[],
729
906
  proposerAddress: EthAddress | undefined,
730
- options: BlockProposalOptions,
907
+ options: BlockProposalOptions = {},
731
908
  ): 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
- // }
909
+ // Validate that we're not creating a proposal for an older or equal position
910
+ if (this.lastProposedBlock) {
911
+ const lastSlot = this.lastProposedBlock.slotNumber;
912
+ const lastIndex = this.lastProposedBlock.indexWithinCheckpoint;
913
+ const newSlot = blockHeader.globalVariables.slotNumber;
914
+
915
+ if (newSlot < lastSlot || (newSlot === lastSlot && indexWithinCheckpoint <= lastIndex)) {
916
+ throw new Error(
917
+ `Cannot create block proposal for slot ${newSlot} index ${indexWithinCheckpoint}: ` +
918
+ `already proposed block for slot ${lastSlot} index ${lastIndex}`,
919
+ );
920
+ }
921
+ }
737
922
 
738
923
  this.log.info(
739
924
  `Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`,
@@ -750,25 +935,42 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
750
935
  broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal,
751
936
  },
752
937
  );
753
- this.previousProposal = newProposal;
938
+ this.lastProposedBlock = newProposal;
754
939
  return newProposal;
755
940
  }
756
941
 
757
942
  async createCheckpointProposal(
758
943
  checkpointHeader: CheckpointHeader,
759
944
  archive: Fr,
945
+ feeAssetPriceModifier: bigint,
760
946
  lastBlockInfo: CreateCheckpointProposalLastBlockData | undefined,
761
947
  proposerAddress: EthAddress | undefined,
762
- options: CheckpointProposalOptions,
948
+ options: CheckpointProposalOptions = {},
763
949
  ): Promise<CheckpointProposal> {
950
+ // Validate that we're not creating a proposal for an older or equal slot
951
+ if (this.lastProposedCheckpoint) {
952
+ const lastSlot = this.lastProposedCheckpoint.slotNumber;
953
+ const newSlot = checkpointHeader.slotNumber;
954
+
955
+ if (newSlot <= lastSlot) {
956
+ throw new Error(
957
+ `Cannot create checkpoint proposal for slot ${newSlot}: ` +
958
+ `already proposed checkpoint for slot ${lastSlot}`,
959
+ );
960
+ }
961
+ }
962
+
764
963
  this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
765
- return await this.validationService.createCheckpointProposal(
964
+ const newProposal = await this.validationService.createCheckpointProposal(
766
965
  checkpointHeader,
767
966
  archive,
967
+ feeAssetPriceModifier,
768
968
  lastBlockInfo,
769
969
  proposerAddress,
770
970
  options,
771
971
  );
972
+ this.lastProposedCheckpoint = newProposal;
973
+ return newProposal;
772
974
  }
773
975
 
774
976
  async broadcastBlockProposal(proposal: BlockProposal): Promise<void> {
@@ -778,8 +980,10 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
778
980
  async signAttestationsAndSigners(
779
981
  attestationsAndSigners: CommitteeAttestationsAndSigners,
780
982
  proposer: EthAddress,
983
+ slot: SlotNumber,
984
+ blockNumber: BlockNumber | CheckpointNumber,
781
985
  ): Promise<Signature> {
782
- return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
986
+ return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber);
783
987
  }
784
988
 
785
989
  async collectOwnAttestations(proposal: CheckpointProposal): Promise<CheckpointAttestation[]> {
@@ -788,6 +992,10 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
788
992
  this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, { inCommittee });
789
993
  const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
790
994
 
995
+ if (!attestations) {
996
+ return [];
997
+ }
998
+
791
999
  // We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
792
1000
  // other nodes can see that our validators did attest to this block proposal, and do not slash us
793
1001
  // due to inactivity for missed attestations.
@@ -886,7 +1094,9 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
886
1094
  }
887
1095
 
888
1096
  const payloadToSign = authRequest.getPayloadToSign();
889
- const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign);
1097
+ // AUTH_REQUEST doesn't require HA protection - multiple signatures are safe
1098
+ const context: SigningContext = { dutyType: DutyType.AUTH_REQUEST };
1099
+ const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign, context);
890
1100
  const authResponse = new AuthResponse(statusMessage, signature);
891
1101
  return authResponse.toBuffer();
892
1102
  }