@aztec/validator-client 0.0.1-commit.2ed92850 → 0.0.1-commit.2f68f620

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 (54) hide show
  1. package/README.md +61 -18
  2. package/dest/checkpoint_builder.d.ts +26 -14
  3. package/dest/checkpoint_builder.d.ts.map +1 -1
  4. package/dest/checkpoint_builder.js +136 -45
  5. package/dest/config.d.ts +1 -1
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +39 -10
  8. package/dest/duties/validation_service.d.ts +12 -13
  9. package/dest/duties/validation_service.d.ts.map +1 -1
  10. package/dest/duties/validation_service.js +33 -45
  11. package/dest/factory.d.ts +10 -4
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +11 -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.d.ts +1 -1
  18. package/dest/key_store/ha_key_store.d.ts.map +1 -1
  19. package/dest/key_store/ha_key_store.js +3 -3
  20. package/dest/metrics.d.ts +16 -3
  21. package/dest/metrics.d.ts.map +1 -1
  22. package/dest/metrics.js +58 -5
  23. package/dest/proposal_handler.d.ts +134 -0
  24. package/dest/proposal_handler.d.ts.map +1 -0
  25. package/dest/proposal_handler.js +1072 -0
  26. package/dest/validator.d.ts +58 -24
  27. package/dest/validator.d.ts.map +1 -1
  28. package/dest/validator.js +363 -235
  29. package/package.json +19 -17
  30. package/src/checkpoint_builder.ts +183 -50
  31. package/src/config.ts +39 -9
  32. package/src/duties/validation_service.ts +59 -54
  33. package/src/factory.ts +20 -3
  34. package/src/index.ts +1 -2
  35. package/src/key_store/ha_key_store.ts +3 -3
  36. package/src/metrics.ts +81 -6
  37. package/src/proposal_handler.ts +1161 -0
  38. package/src/validator.ts +490 -284
  39. package/dest/block_proposal_handler.d.ts +0 -64
  40. package/dest/block_proposal_handler.d.ts.map +0 -1
  41. package/dest/block_proposal_handler.js +0 -545
  42. package/dest/tx_validator/index.d.ts +0 -3
  43. package/dest/tx_validator/index.d.ts.map +0 -1
  44. package/dest/tx_validator/index.js +0 -2
  45. package/dest/tx_validator/nullifier_cache.d.ts +0 -14
  46. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  47. package/dest/tx_validator/nullifier_cache.js +0 -24
  48. package/dest/tx_validator/tx_validator_factory.d.ts +0 -18
  49. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  50. package/dest/tx_validator/tx_validator_factory.js +0 -54
  51. package/src/block_proposal_handler.ts +0 -554
  52. package/src/tx_validator/index.ts +0 -2
  53. package/src/tx_validator/nullifier_cache.ts +0 -30
  54. package/src/tx_validator/tx_validator_factory.ts +0 -135
package/src/validator.ts CHANGED
@@ -1,72 +1,111 @@
1
1
  import type { BlobClientInterface } from '@aztec/blob-client/client';
2
2
  import { type Blob, getBlobsPerL1Block } from '@aztec/blob-lib';
3
3
  import type { EpochCache } from '@aztec/epoch-cache';
4
- import {
5
- BlockNumber,
6
- CheckpointNumber,
7
- EpochNumber,
8
- IndexWithinCheckpoint,
9
- SlotNumber,
10
- } from '@aztec/foundation/branded-types';
4
+ import { CheckpointNumber, EpochNumber, IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
11
5
  import { Fr } from '@aztec/foundation/curves/bn254';
12
- import { TimeoutError } from '@aztec/foundation/error';
13
6
  import type { EthAddress } from '@aztec/foundation/eth-address';
14
7
  import type { Signature } from '@aztec/foundation/eth-signature';
8
+ import { FifoSet } from '@aztec/foundation/fifo-set';
15
9
  import { type LogData, type Logger, createLogger } from '@aztec/foundation/log';
16
- import { retryUntil } from '@aztec/foundation/retry';
17
10
  import { RunningPromise } from '@aztec/foundation/running-promise';
18
11
  import { sleep } from '@aztec/foundation/sleep';
19
12
  import { DateProvider } from '@aztec/foundation/timer';
20
13
  import type { KeystoreManager } from '@aztec/node-keystore';
21
- import type { P2P, PeerId, TxProvider } from '@aztec/p2p';
14
+ import type { DuplicateAttestationInfo, DuplicateProposalInfo, P2P, PeerId } from '@aztec/p2p';
22
15
  import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
23
- import { OffenseType, WANT_TO_SLASH_EVENT, type Watcher, type WatcherEmitter } from '@aztec/slasher';
16
+ import {
17
+ OffenseType,
18
+ WANT_TO_CLEAR_SLASH_EVENT,
19
+ WANT_TO_SLASH_EVENT,
20
+ type Watcher,
21
+ type WatcherEmitter,
22
+ getOffenseTypeName,
23
+ } from '@aztec/slasher';
24
24
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
25
- import type { CommitteeAttestationsAndSigners, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
25
+ import type { CommitteeAttestationsAndSigners, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
26
+ import type { CheckpointReexecutionTracker } from '@aztec/stdlib/checkpoint';
26
27
  import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
27
28
  import type {
28
- CreateCheckpointProposalLastBlockData,
29
+ ITxProvider,
29
30
  Validator,
30
31
  ValidatorClientFullConfig,
31
32
  WorldStateSynchronizer,
32
33
  } from '@aztec/stdlib/interfaces/server';
33
34
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
34
- import type {
35
- BlockProposal,
36
- BlockProposalOptions,
37
- CheckpointAttestation,
38
- CheckpointProposalCore,
39
- CheckpointProposalOptions,
35
+ import {
36
+ type BlockProposal,
37
+ type BlockProposalOptions,
38
+ type CheckpointAttestation,
39
+ CheckpointProposal,
40
+ type CheckpointProposalCore,
41
+ type CheckpointProposalOptions,
42
+ type CoordinationSignatureContext,
40
43
  } from '@aztec/stdlib/p2p';
41
- import { CheckpointProposal } from '@aztec/stdlib/p2p';
42
44
  import type { CheckpointHeader } from '@aztec/stdlib/rollup';
43
- import type { BlockHeader, CheckpointGlobalVariables, Tx } from '@aztec/stdlib/tx';
45
+ import type { BlockHeader, Tx } from '@aztec/stdlib/tx';
44
46
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
45
47
  import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
46
- import { createHASigner } from '@aztec/validator-ha-signer/factory';
47
- import { DutyType, type SigningContext } from '@aztec/validator-ha-signer/types';
48
+ import {
49
+ createHASigner,
50
+ createLocalSignerWithProtection,
51
+ createSignerFromSharedDb,
52
+ } from '@aztec/validator-ha-signer/factory';
53
+ import { DutyType, type SigningContext, type SlashingProtectionDatabase } from '@aztec/validator-ha-signer/types';
54
+ import type { ValidatorHASigner } from '@aztec/validator-ha-signer/validator-ha-signer';
48
55
 
49
56
  import { EventEmitter } from 'events';
50
57
  import type { TypedDataDefinition } from 'viem';
51
58
 
52
- import { BlockProposalHandler, type BlockProposalValidationFailureReason } from './block_proposal_handler.js';
53
59
  import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
54
60
  import { ValidationService } from './duties/validation_service.js';
55
61
  import { HAKeyStore } from './key_store/ha_key_store.js';
56
62
  import type { ExtendedValidatorKeyStore } from './key_store/interface.js';
57
63
  import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
58
64
  import { ValidatorMetrics } from './metrics.js';
65
+ import {
66
+ type BlockProposalValidationFailureReason,
67
+ type CheckpointProposalValidationFailureReason,
68
+ type CheckpointProposalValidationFailureResult,
69
+ ProposalHandler,
70
+ } from './proposal_handler.js';
59
71
 
60
72
  // We maintain a set of proposers who have proposed invalid blocks.
61
73
  // Just cap the set to avoid unbounded growth.
62
74
  const MAX_PROPOSERS_OF_INVALID_BLOCKS = 1000;
75
+ const MAX_TRACKED_INVALID_PROPOSAL_SLOTS = 1000;
76
+ const MAX_TRACKED_INVALID_CHECKPOINT_PROPOSALS = 1000;
77
+ const MAX_TRACKED_BAD_ATTESTATIONS = 10_000;
63
78
 
64
79
  // What errors from the block proposal handler result in slashing
65
80
  const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT: BlockProposalValidationFailureReason[] = [
66
81
  'state_mismatch',
67
82
  'failed_txs',
83
+ 'global_variables_mismatch',
84
+ 'invalid_proposal',
85
+ 'parent_block_wrong_slot',
86
+ 'in_hash_mismatch',
68
87
  ];
69
88
 
89
+ const SLASHABLE_CHECKPOINT_PROPOSAL_VALIDATION_RESULT: Record<CheckpointProposalValidationFailureReason, boolean> = {
90
+ // enabled
91
+ ['invalid_fee_asset_price_modifier']: true,
92
+ ['checkpoint_header_mismatch']: true,
93
+ // These late mismatches should normally be caught by earlier checks, but if reached after validating the local
94
+ // checkpoint inputs, the proposer-signed payload disagrees with deterministic recomputation.
95
+ ['archive_mismatch']: true,
96
+ ['out_hash_mismatch']: true,
97
+ ['no_blocks_for_slot']: true,
98
+ ['too_many_blocks_in_checkpoint']: true,
99
+ ['checkpoint_validation_failed']: true,
100
+ ['last_block_archive_mismatch']: true,
101
+
102
+ // disabled
103
+ ['invalid_signature']: false,
104
+ ['last_block_not_found']: false,
105
+ ['block_fetch_error']: false,
106
+ ['checkpoint_already_published']: false,
107
+ };
108
+
70
109
  /**
71
110
  * Validator Client
72
111
  */
@@ -75,34 +114,41 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
75
114
  private validationService: ValidationService;
76
115
  private metrics: ValidatorMetrics;
77
116
  private log: Logger;
78
-
79
117
  // Whether it has already registered handlers on the p2p client
80
118
  private hasRegisteredHandlers = false;
81
119
 
82
- // Used to check if we are sending the same proposal twice
83
- private previousProposal?: BlockProposal;
120
+ /** Tracks the last block proposal we created, to detect duplicate proposal attempts. */
121
+ private lastProposedBlock?: BlockProposal;
122
+
123
+ /** Tracks the last checkpoint proposal we created. */
124
+ private lastProposedCheckpoint?: CheckpointProposal;
84
125
 
85
126
  private lastEpochForCommitteeUpdateLoop: EpochNumber | undefined;
86
127
  private epochCacheUpdateLoop: RunningPromise;
128
+ /** Tracks the last epoch in which each attester successfully submitted at least one attestation. */
129
+ private lastAttestedEpochByAttester: Map<string, EpochNumber> = new Map();
87
130
 
88
- private proposersOfInvalidBlocks: Set<string> = new Set();
131
+ private proposersOfInvalidBlocks = FifoSet.withLimit<string>(MAX_PROPOSERS_OF_INVALID_BLOCKS);
132
+ private slotsWithInvalidProposals = FifoSet.withLimit<SlotNumber>(MAX_TRACKED_INVALID_PROPOSAL_SLOTS);
133
+ private invalidCheckpointProposalOffenseKeys = FifoSet.withLimit<string>(MAX_TRACKED_INVALID_CHECKPOINT_PROPOSALS);
134
+ private badAttestationOffenseKeys = FifoSet.withLimit<string>(MAX_TRACKED_BAD_ATTESTATIONS);
135
+ private slotsWithProposalEquivocation = FifoSet.withLimit<SlotNumber>(MAX_TRACKED_INVALID_PROPOSAL_SLOTS);
89
136
 
90
- // TODO(palla/mbps): Remove this once checkpoint validation is stable and we can validate all blocks properly.
91
- // Tracks slots for which we have successfully validated a block proposal, so we can attest to checkpoint proposals for those slots.
92
- // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
93
- private validatedBlockSlots: Set<SlotNumber> = new Set();
137
+ /** Tracks the last checkpoint proposal we attested to, to prevent equivocation. */
138
+ private lastAttestedProposal?: CheckpointProposalCore;
94
139
 
95
140
  protected constructor(
96
141
  private keyStore: ExtendedValidatorKeyStore,
97
142
  private epochCache: EpochCache,
98
143
  private p2pClient: P2P,
99
- private blockProposalHandler: BlockProposalHandler,
144
+ private proposalHandler: ProposalHandler,
100
145
  private blockSource: L2BlockSource,
101
146
  private checkpointsBuilder: FullNodeCheckpointsBuilder,
102
147
  private worldState: WorldStateSynchronizer,
103
148
  private l1ToL2MessageSource: L1ToL2MessageSource,
104
149
  private config: ValidatorClientFullConfig,
105
150
  private blobClient: BlobClientInterface,
151
+ private slashingProtectionSigner: ValidatorHASigner,
106
152
  private dateProvider: DateProvider = new DateProvider(),
107
153
  telemetry: TelemetryClient = getTelemetryClient(),
108
154
  log = createLogger('validator'),
@@ -115,11 +161,17 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
115
161
  this.tracer = telemetry.getTracer('Validator');
116
162
  this.metrics = new ValidatorMetrics(telemetry);
117
163
 
118
- this.validationService = new ValidationService(keyStore, this.log.createChild('validation-service'));
164
+ this.validationService = new ValidationService(
165
+ keyStore,
166
+ this.getSignatureContext(),
167
+ this.log.createChild('validation-service'),
168
+ );
169
+ this.proposalHandler.setCheckpointProposalValidationFailureCallback((proposal, result, proposalInfo) =>
170
+ this.handleInvalidCheckpointProposal(proposal, result, proposalInfo),
171
+ );
119
172
 
120
173
  // Refresh epoch cache every second to trigger alert if participation in committee changes
121
174
  this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), this.log, 1000);
122
-
123
175
  const myAddresses = this.getValidatorAddresses();
124
176
  this.log.verbose(`Initialized validator with addresses: ${myAddresses.map(a => a.toString()).join(', ')}`);
125
177
  }
@@ -156,6 +208,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
156
208
  this.log.trace(`No committee found for slot`);
157
209
  return;
158
210
  }
211
+ this.metrics.setCurrentEpoch(epoch);
159
212
  if (epoch !== this.lastEpochForCommitteeUpdateLoop) {
160
213
  const me = this.getValidatorAddresses();
161
214
  const committeeSet = new Set(committee.map(v => v.toString()));
@@ -184,17 +237,26 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
184
237
  p2pClient: P2P,
185
238
  blockSource: L2BlockSource & L2BlockSink,
186
239
  l1ToL2MessageSource: L1ToL2MessageSource,
187
- txProvider: TxProvider,
240
+ txProvider: ITxProvider,
188
241
  keyStoreManager: KeystoreManager,
189
242
  blobClient: BlobClientInterface,
243
+ reexecutionTracker: CheckpointReexecutionTracker,
190
244
  dateProvider: DateProvider = new DateProvider(),
191
245
  telemetry: TelemetryClient = getTelemetryClient(),
246
+ slashingProtectionDb?: SlashingProtectionDatabase,
192
247
  ) {
193
248
  const metrics = new ValidatorMetrics(telemetry);
194
249
  const blockProposalValidator = new BlockProposalValidator(epochCache, {
195
250
  txsPermitted: !config.disableTransactions,
251
+ maxTxsPerBlock: config.validateMaxTxsPerBlock,
252
+ maxBlocksPerCheckpoint: config.maxBlocksPerCheckpoint,
253
+ skipSlotValidation: config.skipProposalSlotValidation,
254
+ signatureContext: {
255
+ chainId: config.l1ChainId,
256
+ rollupAddress: config.rollupAddress,
257
+ },
196
258
  });
197
- const blockProposalHandler = new BlockProposalHandler(
259
+ const proposalHandler = new ProposalHandler(
198
260
  checkpointsBuilder,
199
261
  worldState,
200
262
  blockSource,
@@ -203,33 +265,55 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
203
265
  blockProposalValidator,
204
266
  epochCache,
205
267
  config,
268
+ blobClient,
269
+ reexecutionTracker,
206
270
  metrics,
207
271
  dateProvider,
208
272
  telemetry,
273
+ undefined,
209
274
  );
210
275
 
211
- let validatorKeyStore: ExtendedValidatorKeyStore = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
212
- if (config.haSigningEnabled) {
276
+ const nodeKeystoreAdapter = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
277
+ let slashingProtectionSigner: ValidatorHASigner;
278
+ if (slashingProtectionDb) {
279
+ // Shared database mode: use a pre-existing database (e.g. for testing HA setups).
280
+ ({ signer: slashingProtectionSigner } = createSignerFromSharedDb(slashingProtectionDb, config, {
281
+ telemetryClient: telemetry,
282
+ dateProvider,
283
+ }));
284
+ } else if (config.haSigningEnabled) {
285
+ // Multi-node HA mode: use PostgreSQL-backed distributed locking.
213
286
  // If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
214
287
  const haConfig = {
215
288
  ...config,
216
289
  maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000,
217
290
  };
218
- const { signer } = await createHASigner(haConfig);
219
- validatorKeyStore = new HAKeyStore(validatorKeyStore, signer);
291
+ ({ signer: slashingProtectionSigner } = await createHASigner(haConfig, {
292
+ telemetryClient: telemetry,
293
+ dateProvider,
294
+ }));
295
+ } else {
296
+ // Single-node mode: use LMDB-backed local signing protection.
297
+ // This prevents double-signing if the node crashes and restarts mid-proposal.
298
+ ({ signer: slashingProtectionSigner } = await createLocalSignerWithProtection(config, {
299
+ telemetryClient: telemetry,
300
+ dateProvider,
301
+ }));
220
302
  }
303
+ const validatorKeyStore: ExtendedValidatorKeyStore = new HAKeyStore(nodeKeystoreAdapter, slashingProtectionSigner);
221
304
 
222
305
  const validator = new ValidatorClient(
223
306
  validatorKeyStore,
224
307
  epochCache,
225
308
  p2pClient,
226
- blockProposalHandler,
309
+ proposalHandler,
227
310
  blockSource,
228
311
  checkpointsBuilder,
229
312
  worldState,
230
313
  l1ToL2MessageSource,
231
314
  config,
232
315
  blobClient,
316
+ slashingProtectionSigner,
233
317
  dateProvider,
234
318
  telemetry,
235
319
  );
@@ -243,14 +327,21 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
243
327
  .filter(addr => !this.config.disabledValidators.some(disabled => disabled.equals(addr)));
244
328
  }
245
329
 
246
- public getBlockProposalHandler() {
247
- return this.blockProposalHandler;
330
+ public getProposalHandler() {
331
+ return this.proposalHandler;
248
332
  }
249
333
 
250
334
  public signWithAddress(addr: EthAddress, msg: TypedDataDefinition, context: SigningContext) {
251
335
  return this.keyStore.signTypedDataWithAddress(addr, msg, context);
252
336
  }
253
337
 
338
+ private getSignatureContext(): CoordinationSignatureContext {
339
+ return {
340
+ chainId: this.config.l1ChainId,
341
+ rollupAddress: this.config.rollupAddress,
342
+ };
343
+ }
344
+
254
345
  public getCoinbaseForAttestor(attestor: EthAddress): EthAddress {
255
346
  return this.keyStore.getCoinbaseAddress(attestor);
256
347
  }
@@ -263,8 +354,27 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
263
354
  return this.config;
264
355
  }
265
356
 
357
+ public hasProposalEquivocation(slotNumber: SlotNumber): boolean {
358
+ return this.slotsWithProposalEquivocation.has(slotNumber);
359
+ }
360
+
361
+ public hasInvalidProposals(slotNumber: SlotNumber): boolean {
362
+ return this.slotsWithInvalidProposals.has(slotNumber);
363
+ }
364
+
266
365
  public updateConfig(config: Partial<ValidatorClientFullConfig>) {
267
366
  this.config = { ...this.config, ...config };
367
+ this.proposalHandler.updateConfig(config);
368
+ }
369
+
370
+ public reloadKeystore(newManager: KeystoreManager): void {
371
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
372
+ this.keyStore = new HAKeyStore(newAdapter, this.slashingProtectionSigner);
373
+ this.validationService = new ValidationService(
374
+ this.keyStore,
375
+ this.getSignatureContext(),
376
+ this.log.createChild('validation-service'),
377
+ );
268
378
  }
269
379
 
270
380
  public async start() {
@@ -311,7 +421,21 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
311
421
  checkpoint: CheckpointProposalCore,
312
422
  proposalSender: PeerId,
313
423
  ): Promise<CheckpointAttestation[] | undefined> => this.attestToCheckpointProposal(checkpoint, proposalSender);
314
- this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
424
+ this.p2pClient.registerValidatorCheckpointProposalHandler(checkpointHandler);
425
+
426
+ // Duplicate proposal handler - triggers slashing for equivocation
427
+ this.p2pClient.registerDuplicateProposalCallback((info: DuplicateProposalInfo) => {
428
+ this.handleDuplicateProposal(info);
429
+ });
430
+
431
+ // Duplicate attestation handler - triggers slashing for attestation equivocation
432
+ this.p2pClient.registerDuplicateAttestationCallback((info: DuplicateAttestationInfo) => {
433
+ this.handleDuplicateAttestation(info);
434
+ });
435
+
436
+ this.p2pClient.registerCheckpointAttestationCallback((attestation: CheckpointAttestation) => {
437
+ this.handleCheckpointAttestation(attestation);
438
+ });
315
439
 
316
440
  const myAddresses = this.getValidatorAddresses();
317
441
  this.p2pClient.registerThisValidatorAddresses(myAddresses);
@@ -340,6 +464,14 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
340
464
  return false;
341
465
  }
342
466
 
467
+ // Log self-proposals from HA peers (same validator key on different nodes)
468
+ if (this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
469
+ this.log.verbose(`Processing block proposal from HA peer for slot ${slotNumber}`, {
470
+ proposer: proposer.toString(),
471
+ slotNumber,
472
+ });
473
+ }
474
+
343
475
  // Check if we're in the committee (for metrics purposes)
344
476
  const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
345
477
  const partOfCommittee = inCommittee.length > 0;
@@ -351,27 +483,14 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
351
483
  fishermanMode: this.config.fishermanMode || false,
352
484
  });
353
485
 
354
- // Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
355
- // In fisherman mode, we always reexecute to validate proposals.
356
- const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } =
357
- this.config;
358
- const shouldReexecute =
359
- fishermanMode ||
360
- (slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute) ||
361
- (partOfCommittee && validatorReexecute) ||
362
- alwaysReexecuteBlockProposals ||
363
- this.blobClient.canUpload();
364
-
365
- const validationResult = await this.blockProposalHandler.handleBlockProposal(
366
- proposal,
367
- proposalSender,
368
- !!shouldReexecute && !escapeHatchOpen,
369
- );
486
+ // Reexecute outside the escape hatch so slashing observers can detect invalid proposals even when penalties are 0.
487
+ const validationResult = await this.proposalHandler.handleBlockProposal(proposal, proposalSender, !escapeHatchOpen);
370
488
 
371
489
  if (!validationResult.isValid) {
372
- this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
373
-
374
490
  const reason = validationResult.reason || 'unknown';
491
+
492
+ this.log.warn(`Block proposal validation failed: ${reason}`, proposalInfo);
493
+
375
494
  // Classify failure reason: bad proposal vs node issue
376
495
  const badProposalReasons: BlockProposalValidationFailureReason[] = [
377
496
  'invalid_proposal',
@@ -388,15 +507,18 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
388
507
  this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
389
508
  }
390
509
 
391
- // Slash invalid block proposals (can happen even when not in committee)
392
510
  if (
393
511
  !escapeHatchOpen &&
394
512
  validationResult.reason &&
395
- SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) &&
396
- slashBroadcastedInvalidBlockPenalty > 0n
513
+ SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason)
397
514
  ) {
398
- this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
515
+ this.log.info(`Detected invalid block proposal offense`, {
516
+ ...proposalInfo,
517
+ amount: this.config.slashBroadcastedInvalidBlockPenalty,
518
+ offenseType: getOffenseTypeName(OffenseType.BROADCASTED_INVALID_BLOCK_PROPOSAL),
519
+ });
399
520
  this.slashInvalidBlock(proposal);
521
+ this.markInvalidProposalSlot(proposal.slotNumber);
400
522
  }
401
523
  return false;
402
524
  }
@@ -413,10 +535,6 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
413
535
  return false;
414
536
  }
415
537
 
416
- // TODO(palla/mbps): Remove this once checkpoint validation is stable.
417
- // Track that we successfully validated a block for this slot, so we can attest to checkpoint proposals for it.
418
- this.validatedBlockSlots.add(slotNumber);
419
-
420
538
  return true;
421
539
  }
422
540
 
@@ -430,59 +548,56 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
430
548
  proposal: CheckpointProposalCore,
431
549
  _proposalSender: PeerId,
432
550
  ): Promise<CheckpointAttestation[] | undefined> {
433
- const slotNumber = proposal.slotNumber;
551
+ const proposalSlotNumber = proposal.slotNumber;
434
552
  const proposer = proposal.getSender();
435
553
 
436
554
  // If escape hatch is open for this slot's epoch, do not attest.
437
- if (await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber)) {
438
- this.log.warn(`Escape hatch open for slot ${slotNumber}, skipping checkpoint attestation handling`);
555
+ if (await this.epochCache.isEscapeHatchOpenAtSlot(proposalSlotNumber)) {
556
+ this.log.warn(`Escape hatch open for slot ${proposalSlotNumber}, skipping checkpoint attestation handling`);
439
557
  return undefined;
440
558
  }
441
559
 
442
- // Reject proposals with invalid signatures
443
- if (!proposer) {
444
- this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
560
+ // Early-out for equivocation: refuses if we've already attested to a higher slot.
561
+ if (!this.shouldAttestToSlot(proposalSlotNumber)) {
445
562
  return undefined;
446
563
  }
447
564
 
448
- // Check that I have any address in current committee before attesting
449
- const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
565
+ // Ignore proposals from ourselves (may happen in HA setups)
566
+ if (proposer && this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
567
+ this.log.debug(`Ignoring block proposal from self for slot ${proposalSlotNumber}`, {
568
+ proposer: proposer.toString(),
569
+ proposalSlotNumber,
570
+ });
571
+ return undefined;
572
+ }
573
+
574
+ // Check that I have any address in the committee where this checkpoint will land before attesting
575
+ const inCommittee = await this.epochCache.filterInCommittee(proposalSlotNumber, this.getValidatorAddresses());
450
576
  const partOfCommittee = inCommittee.length > 0;
451
577
 
452
578
  const proposalInfo = {
453
- slotNumber,
579
+ proposalSlotNumber,
454
580
  archive: proposal.archive.toString(),
455
- proposer: proposer.toString(),
456
- txCount: proposal.txHashes.length,
581
+ proposer: proposer?.toString(),
457
582
  };
458
- this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
583
+ this.log.info(`Received checkpoint proposal for slot ${proposalSlotNumber}`, {
459
584
  ...proposalInfo,
460
- txHashes: proposal.txHashes.map(t => t.toString()),
461
585
  fishermanMode: this.config.fishermanMode || false,
462
586
  });
463
587
 
464
- // TODO(palla/mbps): Remove this once checkpoint validation is stable.
465
- // Check that we have successfully validated a block for this slot before attesting to the checkpoint.
466
- if (!this.validatedBlockSlots.has(slotNumber)) {
467
- this.log.warn(`No validated block found for slot ${slotNumber}, refusing to attest to checkpoint`, proposalInfo);
468
- return undefined;
469
- }
470
-
471
- // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
472
- // TODO(palla/mbps): Change default to false once checkpoint validation is stable.
473
- if (this.config.skipCheckpointProposalValidation !== false) {
474
- this.log.verbose(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
588
+ // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set).
589
+ // Uses the cached result from the all-nodes callback if available (avoids double validation).
590
+ let checkpointNumber: CheckpointNumber;
591
+ if (this.config.skipCheckpointProposalValidation) {
592
+ this.log.warn(`Skipping checkpoint proposal validation for slot ${proposalSlotNumber}`, proposalInfo);
593
+ checkpointNumber = CheckpointNumber(0);
475
594
  } else {
476
- const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
595
+ const validationResult = await this.proposalHandler.handleCheckpointProposal(proposal, proposalInfo);
477
596
  if (!validationResult.isValid) {
478
597
  this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
479
598
  return undefined;
480
599
  }
481
- }
482
-
483
- // Upload blobs to filestore if we can (fire and forget)
484
- if (this.blobClient.canUpload()) {
485
- void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
600
+ checkpointNumber = validationResult.checkpointNumber;
486
601
  }
487
602
 
488
603
  // Check that I have any address in current committee before attesting
@@ -493,14 +608,28 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
493
608
  }
494
609
 
495
610
  // Provided all of the above checks pass, we can attest to the proposal
496
- this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
497
- ...proposalInfo,
498
- inCommittee: partOfCommittee,
499
- fishermanMode: this.config.fishermanMode || false,
500
- });
611
+ this.log.info(
612
+ `${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${proposalSlotNumber}`,
613
+ {
614
+ ...proposalInfo,
615
+ inCommittee: partOfCommittee,
616
+ fishermanMode: this.config.fishermanMode || false,
617
+ },
618
+ );
501
619
 
502
620
  this.metrics.incSuccessfulAttestations(inCommittee.length);
503
621
 
622
+ // Track epoch participation per attester: count each (attester, epoch) pair at most once
623
+ const proposalEpoch = getEpochAtSlot(proposalSlotNumber, this.epochCache.getL1Constants());
624
+ for (const attester of inCommittee) {
625
+ const key = attester.toString();
626
+ const lastEpoch = this.lastAttestedEpochByAttester.get(key);
627
+ if (lastEpoch === undefined || proposalEpoch > lastEpoch) {
628
+ this.lastAttestedEpochByAttester.set(key, proposalEpoch);
629
+ this.metrics.incAttestedEpochCount(attester);
630
+ }
631
+ }
632
+
504
633
  // Determine which validators should attest
505
634
  let attestors: EthAddress[];
506
635
  if (partOfCommittee) {
@@ -519,169 +648,62 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
519
648
 
520
649
  if (this.config.fishermanMode) {
521
650
  // bail out early and don't save attestations to the pool in fisherman mode
522
- this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
651
+ this.log.info(`Creating checkpoint attestations for slot ${proposalSlotNumber}`, {
523
652
  ...proposalInfo,
524
653
  attestors: attestors.map(a => a.toString()),
525
654
  });
526
655
  return undefined;
527
656
  }
528
657
 
529
- return this.createCheckpointAttestationsFromProposal(proposal, attestors);
530
- }
531
-
532
- private async createCheckpointAttestationsFromProposal(
533
- proposal: CheckpointProposalCore,
534
- attestors: EthAddress[] = [],
535
- ): Promise<CheckpointAttestation[]> {
536
- const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
537
- await this.p2pClient.addCheckpointAttestations(attestations);
538
- return attestations;
658
+ return await this.createCheckpointAttestationsFromProposal(proposal, attestors, checkpointNumber);
539
659
  }
540
660
 
541
661
  /**
542
- * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
543
- * @returns Validation result with isValid flag and reason if invalid.
662
+ * Checks if we should attest to a slot based on equivocation prevention rules.
663
+ * @returns true if we should attest, false if we should skip
544
664
  */
545
- private async validateCheckpointProposal(
546
- proposal: CheckpointProposalCore,
547
- proposalInfo: LogData,
548
- ): Promise<{ isValid: true } | { isValid: false; reason: string }> {
549
- const slot = proposal.slotNumber;
550
- const timeoutSeconds = 10;
665
+ private shouldAttestToSlot(slotNumber: SlotNumber): boolean {
666
+ // If attestToEquivocatedProposals is true, always allow
667
+ if (this.config.attestToEquivocatedProposals) {
668
+ return true;
669
+ }
551
670
 
552
- // Wait for last block to sync by archive
553
- let lastBlockHeader: BlockHeader | undefined;
554
- try {
555
- lastBlockHeader = await retryUntil(
556
- async () => {
557
- await this.blockSource.syncImmediate();
558
- return this.blockSource.getBlockHeaderByArchive(proposal.archive);
559
- },
560
- `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`,
561
- timeoutSeconds,
562
- 0.5,
671
+ // Check if incoming slot is strictly greater than last attested
672
+ if (this.lastAttestedProposal && slotNumber <= this.lastAttestedProposal.slotNumber) {
673
+ this.log.warn(
674
+ `Refusing to process a proposal for slot ${slotNumber} given we already attested to a proposal for slot ${this.lastAttestedProposal.slotNumber}`,
563
675
  );
564
- } catch (err) {
565
- if (err instanceof TimeoutError) {
566
- this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
567
- return { isValid: false, reason: 'last_block_not_found' };
568
- }
569
- this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
570
- return { isValid: false, reason: 'block_fetch_error' };
676
+ return false;
571
677
  }
572
678
 
573
- if (!lastBlockHeader) {
574
- this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
575
- return { isValid: false, reason: 'last_block_not_found' };
576
- }
679
+ return true;
680
+ }
577
681
 
578
- // Get all full blocks for the slot and checkpoint
579
- const blocks = await this.blockSource.getBlocksForSlot(slot);
580
- if (blocks.length === 0) {
581
- this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
582
- return { isValid: false, reason: 'no_blocks_for_slot' };
682
+ private async createCheckpointAttestationsFromProposal(
683
+ proposal: CheckpointProposalCore,
684
+ attestors: EthAddress[] = [],
685
+ checkpointNumber: CheckpointNumber,
686
+ ): Promise<CheckpointAttestation[] | undefined> {
687
+ // Equivocation check: must happen right before signing to minimize the race window
688
+ if (!this.shouldAttestToSlot(proposal.slotNumber)) {
689
+ return undefined;
583
690
  }
584
691
 
585
- this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
586
- ...proposalInfo,
587
- blockNumbers: blocks.map(b => b.number),
588
- });
589
-
590
- // Get checkpoint constants from first block
591
- const firstBlock = blocks[0];
592
- const constants = this.extractCheckpointConstants(firstBlock);
593
- const checkpointNumber = firstBlock.checkpointNumber;
594
-
595
- // Get L1-to-L2 messages for this checkpoint
596
- const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
597
-
598
- // Compute the previous checkpoint out hashes for the epoch.
599
- // TODO: There can be a more efficient way to get the previous checkpoint out hashes without having to fetch the
600
- // actual checkpoints and the blocks/txs in them.
601
- const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
602
- const previousCheckpoints = (await this.blockSource.getCheckpointsForEpoch(epoch))
603
- .filter(b => b.number < checkpointNumber)
604
- .sort((a, b) => a.number - b.number);
605
- const previousCheckpointOutHashes = previousCheckpoints.map(c => c.getCheckpointOutHash());
606
-
607
- // Fork world state at the block before the first block
608
- const parentBlockNumber = BlockNumber(firstBlock.number - 1);
609
- const fork = await this.worldState.fork(parentBlockNumber);
610
-
611
- try {
612
- // Create checkpoint builder with all existing blocks
613
- const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
614
- checkpointNumber,
615
- constants,
616
- l1ToL2Messages,
617
- previousCheckpointOutHashes,
618
- fork,
619
- blocks,
620
- );
621
-
622
- // Complete the checkpoint to get computed values
623
- const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
624
-
625
- // Compare checkpoint header with proposal
626
- if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
627
- this.log.warn(`Checkpoint header mismatch`, {
628
- ...proposalInfo,
629
- computed: computedCheckpoint.header.toInspect(),
630
- proposal: proposal.checkpointHeader.toInspect(),
631
- });
632
- return { isValid: false, reason: 'checkpoint_header_mismatch' };
633
- }
634
-
635
- // Compare archive root with proposal
636
- if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
637
- this.log.warn(`Archive root mismatch`, {
638
- ...proposalInfo,
639
- computed: computedCheckpoint.archive.root.toString(),
640
- proposal: proposal.archive.toString(),
641
- });
642
- return { isValid: false, reason: 'archive_mismatch' };
643
- }
644
-
645
- // Check that the accumulated out hash matches the value in the proposal.
646
- const computedOutHash = computedCheckpoint.getCheckpointOutHash();
647
- const proposalOutHash = proposal.checkpointHeader.epochOutHash;
648
- if (!computedOutHash.equals(proposalOutHash)) {
649
- this.log.warn(`Epoch out hash mismatch`, {
650
- proposalOutHash: proposalOutHash.toString(),
651
- computedOutHash: computedOutHash.toString(),
652
- ...proposalInfo,
653
- });
654
- return { isValid: false, reason: 'out_hash_mismatch' };
655
- }
692
+ const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors, checkpointNumber);
656
693
 
657
- this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
658
- return { isValid: true };
659
- } finally {
660
- await fork.close();
661
- }
662
- }
694
+ // Track the proposal we attested to (to prevent equivocation)
695
+ this.lastAttestedProposal = proposal;
663
696
 
664
- /**
665
- * Extract checkpoint global variables from a block.
666
- */
667
- private extractCheckpointConstants(block: L2Block): CheckpointGlobalVariables {
668
- const gv = block.header.globalVariables;
669
- return {
670
- chainId: gv.chainId,
671
- version: gv.version,
672
- slotNumber: gv.slotNumber,
673
- coinbase: gv.coinbase,
674
- feeRecipient: gv.feeRecipient,
675
- gasFees: gv.gasFees,
676
- };
697
+ await this.p2pClient.addOwnCheckpointAttestations(attestations);
698
+ return attestations;
677
699
  }
678
700
 
679
701
  /**
680
702
  * Uploads blobs for a checkpoint to the filestore (fire and forget).
681
703
  */
682
- private async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
704
+ protected async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
683
705
  try {
684
- const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
706
+ const lastBlockHeader = (await this.blockSource.getBlockData({ archive: proposal.archive }))?.header;
685
707
  if (!lastBlockHeader) {
686
708
  this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
687
709
  return;
@@ -694,7 +716,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
694
716
  }
695
717
 
696
718
  const blobFields = blocks.flatMap(b => b.toBlobFields());
697
- const blobs: Blob[] = getBlobsPerL1Block(blobFields);
719
+ const blobs: Blob[] = await getBlobsPerL1Block(blobFields);
698
720
  await this.blobClient.sendBlobsToFilestore(blobs);
699
721
  this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
700
722
  ...proposalInfo,
@@ -714,12 +736,6 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
714
736
  return;
715
737
  }
716
738
 
717
- // Trim the set if it's too big.
718
- if (this.proposersOfInvalidBlocks.size > MAX_PROPOSERS_OF_INVALID_BLOCKS) {
719
- // remove oldest proposer. `values` is guaranteed to be in insertion order.
720
- this.proposersOfInvalidBlocks.delete(this.proposersOfInvalidBlocks.values().next().value!);
721
- }
722
-
723
739
  this.proposersOfInvalidBlocks.add(proposer.toString());
724
740
 
725
741
  this.emit(WANT_TO_SLASH_EVENT, [
@@ -732,26 +748,186 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
732
748
  ]);
733
749
  }
734
750
 
751
+ private handleInvalidCheckpointProposal(
752
+ proposal: CheckpointProposalCore,
753
+ result: CheckpointProposalValidationFailureResult,
754
+ proposalInfo: LogData,
755
+ ): void {
756
+ if (!SLASHABLE_CHECKPOINT_PROPOSAL_VALIDATION_RESULT[result.reason]) {
757
+ return;
758
+ }
759
+
760
+ this.markInvalidProposalSlot(proposal.slotNumber);
761
+
762
+ if (this.slashInvalidCheckpointProposal(proposal)) {
763
+ this.log.info(`Detected invalid checkpoint proposal offense`, {
764
+ ...proposalInfo,
765
+ reason: result.reason,
766
+ amount: this.config.slashBroadcastedInvalidCheckpointProposalPenalty,
767
+ offenseType: getOffenseTypeName(OffenseType.BROADCASTED_INVALID_CHECKPOINT_PROPOSAL),
768
+ });
769
+ }
770
+ }
771
+
772
+ private slashInvalidCheckpointProposal(proposal: CheckpointProposalCore): boolean {
773
+ const proposer = proposal.getSender();
774
+ if (!proposer) {
775
+ this.log.warn(`Cannot slash checkpoint proposal with invalid signature`, {
776
+ slotNumber: proposal.slotNumber,
777
+ archive: proposal.archive.toString(),
778
+ });
779
+ return false;
780
+ }
781
+
782
+ const offenseType = OffenseType.BROADCASTED_INVALID_CHECKPOINT_PROPOSAL;
783
+ const offenseKey = `${proposer.toString()}:${offenseType}:${proposal.slotNumber}`;
784
+ if (!this.invalidCheckpointProposalOffenseKeys.addIfAbsent(offenseKey)) {
785
+ return false;
786
+ }
787
+
788
+ this.emit(WANT_TO_SLASH_EVENT, [
789
+ {
790
+ validator: proposer,
791
+ amount: this.config.slashBroadcastedInvalidCheckpointProposalPenalty,
792
+ offenseType,
793
+ epochOrSlot: BigInt(proposal.slotNumber),
794
+ },
795
+ ]);
796
+ return true;
797
+ }
798
+
799
+ private markInvalidProposalSlot(slotNumber: SlotNumber): void {
800
+ this.slotsWithInvalidProposals.add(slotNumber);
801
+ }
802
+
803
+ private handleCheckpointAttestation(attestation: CheckpointAttestation): void {
804
+ const slotNumber = attestation.slotNumber;
805
+ if (!this.slotsWithInvalidProposals.has(slotNumber) || this.slotsWithProposalEquivocation.has(slotNumber)) {
806
+ return;
807
+ }
808
+
809
+ const attester = attestation.getSender();
810
+ if (!attester) {
811
+ this.log.warn(`Cannot slash checkpoint attestation with invalid signature`, {
812
+ slotNumber,
813
+ archive: attestation.archive.toString(),
814
+ });
815
+ return;
816
+ }
817
+
818
+ this.slashAttestedToInvalidCheckpointProposal(slotNumber, attester);
819
+ }
820
+
821
+ private slashAttestedToInvalidCheckpointProposal(slotNumber: SlotNumber, attester: EthAddress): void {
822
+ const offenseKey = `${attester.toString()}:${OffenseType.ATTESTED_TO_INVALID_CHECKPOINT_PROPOSAL}:${slotNumber}`;
823
+ if (!this.badAttestationOffenseKeys.addIfAbsent(offenseKey)) {
824
+ return;
825
+ }
826
+
827
+ this.log.info(`Detected attestation to invalid checkpoint proposal offense`, {
828
+ attester: attester.toString(),
829
+ slotNumber,
830
+ amount: this.config.slashAttestInvalidCheckpointProposalPenalty,
831
+ offenseType: getOffenseTypeName(OffenseType.ATTESTED_TO_INVALID_CHECKPOINT_PROPOSAL),
832
+ });
833
+
834
+ this.emit(WANT_TO_SLASH_EVENT, [
835
+ {
836
+ validator: attester,
837
+ amount: this.config.slashAttestInvalidCheckpointProposalPenalty,
838
+ offenseType: OffenseType.ATTESTED_TO_INVALID_CHECKPOINT_PROPOSAL,
839
+ epochOrSlot: BigInt(slotNumber),
840
+ },
841
+ ]);
842
+ }
843
+
844
+ /**
845
+ * Handle detection of a duplicate proposal (equivocation).
846
+ * Emits a slash event when a proposer sends multiple proposals for the same position.
847
+ */
848
+ private handleDuplicateProposal(info: DuplicateProposalInfo): void {
849
+ const { slot, proposer, type } = info;
850
+ this.slotsWithProposalEquivocation.add(slot);
851
+
852
+ this.log.info(`Detected duplicate ${type} proposal offense from ${proposer.toString()} at slot ${slot}`, {
853
+ proposer: proposer.toString(),
854
+ slot,
855
+ type,
856
+ amount: this.config.slashDuplicateProposalPenalty,
857
+ offenseType: getOffenseTypeName(OffenseType.DUPLICATE_PROPOSAL),
858
+ });
859
+
860
+ this.emit(WANT_TO_SLASH_EVENT, [
861
+ {
862
+ validator: proposer,
863
+ amount: this.config.slashDuplicateProposalPenalty,
864
+ offenseType: OffenseType.DUPLICATE_PROPOSAL,
865
+ epochOrSlot: BigInt(slot),
866
+ },
867
+ ]);
868
+
869
+ this.emit(WANT_TO_CLEAR_SLASH_EVENT, [
870
+ {
871
+ offenseType: OffenseType.ATTESTED_TO_INVALID_CHECKPOINT_PROPOSAL,
872
+ epochOrSlot: BigInt(slot),
873
+ },
874
+ ]);
875
+ }
876
+
877
+ /**
878
+ * Handle detection of a duplicate attestation (equivocation).
879
+ * Emits a slash event when an attester signs attestations for different proposals at the same slot.
880
+ */
881
+ private handleDuplicateAttestation(info: DuplicateAttestationInfo): void {
882
+ const { slot, attester } = info;
883
+
884
+ this.log.info(`Detected duplicate attestation offense from ${attester.toString()} at slot ${slot}`, {
885
+ attester: attester.toString(),
886
+ slot,
887
+ amount: this.config.slashDuplicateAttestationPenalty,
888
+ offenseType: getOffenseTypeName(OffenseType.DUPLICATE_ATTESTATION),
889
+ });
890
+
891
+ this.emit(WANT_TO_SLASH_EVENT, [
892
+ {
893
+ validator: attester,
894
+ amount: this.config.slashDuplicateAttestationPenalty,
895
+ offenseType: OffenseType.DUPLICATE_ATTESTATION,
896
+ epochOrSlot: BigInt(slot),
897
+ },
898
+ ]);
899
+ }
900
+
735
901
  async createBlockProposal(
736
902
  blockHeader: BlockHeader,
903
+ checkpointNumber: CheckpointNumber,
737
904
  indexWithinCheckpoint: IndexWithinCheckpoint,
738
905
  inHash: Fr,
739
906
  archive: Fr,
740
907
  txs: Tx[],
741
908
  proposerAddress: EthAddress | undefined,
742
- options: BlockProposalOptions,
909
+ options: BlockProposalOptions = {},
743
910
  ): Promise<BlockProposal> {
744
- // TODO(palla/mbps): Prevent double proposals properly
745
- // if (this.previousProposal?.slotNumber === blockHeader.globalVariables.slotNumber) {
746
- // this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
747
- // return Promise.resolve(undefined);
748
- // }
911
+ // Validate that we're not creating a proposal for an older or equal position
912
+ if (this.lastProposedBlock) {
913
+ const lastSlot = this.lastProposedBlock.slotNumber;
914
+ const lastIndex = this.lastProposedBlock.indexWithinCheckpoint;
915
+ const newSlot = blockHeader.globalVariables.slotNumber;
916
+
917
+ if (newSlot < lastSlot || (newSlot === lastSlot && indexWithinCheckpoint <= lastIndex)) {
918
+ throw new Error(
919
+ `Cannot create block proposal for slot ${newSlot} index ${indexWithinCheckpoint}: ` +
920
+ `already proposed block for slot ${lastSlot} index ${lastIndex}`,
921
+ );
922
+ }
923
+ }
749
924
 
750
925
  this.log.info(
751
926
  `Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`,
752
927
  );
753
928
  const newProposal = await this.validationService.createBlockProposal(
754
929
  blockHeader,
930
+ checkpointNumber,
755
931
  indexWithinCheckpoint,
756
932
  inHash,
757
933
  archive,
@@ -759,28 +935,55 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
759
935
  proposerAddress,
760
936
  {
761
937
  ...options,
762
- broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal,
938
+ broadcastInvalidBlockProposal:
939
+ options.broadcastInvalidBlockProposal || this.config.broadcastInvalidBlockProposal,
763
940
  },
764
941
  );
765
- this.previousProposal = newProposal;
942
+ this.lastProposedBlock = newProposal;
766
943
  return newProposal;
767
944
  }
768
945
 
769
946
  async createCheckpointProposal(
770
947
  checkpointHeader: CheckpointHeader,
771
948
  archive: Fr,
772
- lastBlockInfo: CreateCheckpointProposalLastBlockData | undefined,
949
+ checkpointNumber: CheckpointNumber,
950
+ feeAssetPriceModifier: bigint,
951
+ lastBlockProposal: BlockProposal | undefined,
773
952
  proposerAddress: EthAddress | undefined,
774
- options: CheckpointProposalOptions,
953
+ options: CheckpointProposalOptions = {},
775
954
  ): Promise<CheckpointProposal> {
955
+ // Validate that we're not creating a proposal for an older or equal slot
956
+ if (this.lastProposedCheckpoint) {
957
+ const lastSlot = this.lastProposedCheckpoint.slotNumber;
958
+ const newSlot = checkpointHeader.slotNumber;
959
+
960
+ if (newSlot <= lastSlot) {
961
+ throw new Error(
962
+ `Cannot create checkpoint proposal for slot ${newSlot}: ` +
963
+ `already proposed checkpoint for slot ${lastSlot}`,
964
+ );
965
+ }
966
+ }
967
+
776
968
  this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
777
- return await this.validationService.createCheckpointProposal(
969
+ const newProposal = await this.validationService.createCheckpointProposal(
778
970
  checkpointHeader,
779
971
  archive,
780
- lastBlockInfo,
972
+ checkpointNumber,
973
+ feeAssetPriceModifier,
974
+ lastBlockProposal,
781
975
  proposerAddress,
782
976
  options,
783
977
  );
978
+ this.lastProposedCheckpoint = newProposal;
979
+ // Self-record this slot's outcome on the re-execution tracker. Proposers don't run their
980
+ // own proposals through `handleCheckpointProposal`, so without this call the proposer's
981
+ // sentinel would see no outcome for slots it proposed and would mis-attribute itself as
982
+ // inactive. We pass the locally-computed `archive` (not `newProposal.archive`, which may
983
+ // be intentionally corrupted under test-only flags); from the proposer's local-view
984
+ // perspective the work it just completed is valid by definition.
985
+ this.proposalHandler.recordOwnCheckpointProposalAsValid(checkpointHeader.slotNumber, archive, checkpointNumber);
986
+ return newProposal;
784
987
  }
785
988
 
786
989
  async broadcastBlockProposal(proposal: BlockProposal): Promise<void> {
@@ -791,16 +994,28 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
791
994
  attestationsAndSigners: CommitteeAttestationsAndSigners,
792
995
  proposer: EthAddress,
793
996
  slot: SlotNumber,
794
- blockNumber: BlockNumber | CheckpointNumber,
997
+ checkpointNumber: CheckpointNumber,
795
998
  ): Promise<Signature> {
796
- return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber);
999
+ return await this.validationService.signAttestationsAndSigners(
1000
+ attestationsAndSigners,
1001
+ proposer,
1002
+ slot,
1003
+ checkpointNumber,
1004
+ );
797
1005
  }
798
1006
 
799
- async collectOwnAttestations(proposal: CheckpointProposal): Promise<CheckpointAttestation[]> {
1007
+ async collectOwnAttestations(
1008
+ proposal: CheckpointProposal,
1009
+ checkpointNumber: CheckpointNumber,
1010
+ ): Promise<CheckpointAttestation[]> {
800
1011
  const slot = proposal.slotNumber;
801
1012
  const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
802
1013
  this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, { inCommittee });
803
- const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
1014
+ const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee, checkpointNumber);
1015
+
1016
+ if (!attestations) {
1017
+ return [];
1018
+ }
804
1019
 
805
1020
  // We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
806
1021
  // other nodes can see that our validators did attest to this block proposal, and do not slash us
@@ -815,6 +1030,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
815
1030
  proposal: CheckpointProposal,
816
1031
  required: number,
817
1032
  deadline: Date,
1033
+ checkpointNumber: CheckpointNumber,
818
1034
  ): Promise<CheckpointAttestation[]> {
819
1035
  // Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
820
1036
  const slot = proposal.slotNumber;
@@ -827,33 +1043,23 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
827
1043
  throw new AttestationTimeoutError(0, required, slot);
828
1044
  }
829
1045
 
830
- await this.collectOwnAttestations(proposal);
1046
+ await this.collectOwnAttestations(proposal, checkpointNumber);
831
1047
 
832
- const proposalId = proposal.archive.toString();
1048
+ const proposalPayloadHash = proposal.getPayloadHash();
833
1049
  const myAddresses = this.getValidatorAddresses();
834
1050
 
835
1051
  let attestations: CheckpointAttestation[] = [];
836
1052
  while (true) {
837
- // Filter out attestations with a mismatching archive. This should NOT happen since we have verified
838
- // the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
839
- const collectedAttestations = (await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalId)).filter(
840
- attestation => {
841
- if (!attestation.archive.equals(proposal.archive)) {
842
- this.log.warn(
843
- `Received attestation for slot ${slot} with mismatched archive from ${attestation.getSender()?.toString()}`,
844
- { attestationArchive: attestation.archive.toString(), proposalArchive: proposal.archive.toString() },
845
- );
846
- return false;
847
- }
848
- return true;
849
- },
850
- );
1053
+ // The pool already filters by proposal payload hash; if any attestation slips through with a
1054
+ // mismatched payload hash, drop it defensively. Equivocations are emitted as separate slash
1055
+ // events from libp2p_service.
1056
+ const collectedAttestations = await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalPayloadHash);
851
1057
 
852
1058
  // Log new attestations we collected
853
1059
  const oldSenders = attestations.map(attestation => attestation.getSender());
854
1060
  for (const collected of collectedAttestations) {
855
1061
  const collectedSender = collected.getSender();
856
- // Skip attestations with invalid signatures
1062
+ // Skip attestations with invalid signatures. Should not happen as we don't add invalid attestations to our pool.
857
1063
  if (!collectedSender) {
858
1064
  this.log.warn(`Skipping attestation with invalid signature for slot ${slot}`);
859
1065
  continue;