@aztec/validator-client 0.0.1-commit.0b941701 → 0.0.1-commit.0dc957cde

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 +60 -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 +28 -9
  8. package/dest/duties/validation_service.d.ts +7 -9
  9. package/dest/duties/validation_service.d.ts.map +1 -1
  10. package/dest/duties/validation_service.js +15 -33
  11. package/dest/factory.d.ts +7 -4
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +6 -5
  14. package/dest/index.d.ts +2 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +1 -2
  17. package/dest/key_store/ha_key_store.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 +108 -0
  24. package/dest/proposal_handler.d.ts.map +1 -0
  25. package/dest/proposal_handler.js +974 -0
  26. package/dest/validator.d.ts +45 -24
  27. package/dest/validator.d.ts.map +1 -1
  28. package/dest/validator.js +194 -206
  29. package/package.json +19 -17
  30. package/src/checkpoint_builder.ts +183 -50
  31. package/src/config.ts +28 -9
  32. package/src/duties/validation_service.ts +25 -37
  33. package/src/factory.ts +10 -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 +1042 -0
  38. package/src/validator.ts +272 -241
  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/dest/validator.js CHANGED
@@ -1,8 +1,6 @@
1
1
  import { getBlobsPerL1Block } from '@aztec/blob-lib';
2
- import { BlockNumber } from '@aztec/foundation/branded-types';
3
- import { TimeoutError } from '@aztec/foundation/error';
2
+ import { CheckpointNumber } from '@aztec/foundation/branded-types';
4
3
  import { createLogger } from '@aztec/foundation/log';
5
- import { retryUntil } from '@aztec/foundation/retry';
6
4
  import { RunningPromise } from '@aztec/foundation/running-promise';
7
5
  import { sleep } from '@aztec/foundation/sleep';
8
6
  import { DateProvider } from '@aztec/foundation/timer';
@@ -11,14 +9,14 @@ import { OffenseType, WANT_TO_SLASH_EVENT } from '@aztec/slasher';
11
9
  import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
12
10
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
13
11
  import { getTelemetryClient } from '@aztec/telemetry-client';
14
- import { createHASigner } from '@aztec/validator-ha-signer/factory';
12
+ import { createHASigner, createLocalSignerWithProtection, createSignerFromSharedDb } from '@aztec/validator-ha-signer/factory';
15
13
  import { DutyType } from '@aztec/validator-ha-signer/types';
16
14
  import { EventEmitter } from 'events';
17
- import { BlockProposalHandler } from './block_proposal_handler.js';
18
15
  import { ValidationService } from './duties/validation_service.js';
19
16
  import { HAKeyStore } from './key_store/ha_key_store.js';
20
17
  import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
21
18
  import { ValidatorMetrics } from './metrics.js';
19
+ import { ProposalHandler } from './proposal_handler.js';
22
20
  // We maintain a set of proposers who have proposed invalid blocks.
23
21
  // Just cap the set to avoid unbounded growth.
24
22
  const MAX_PROPOSERS_OF_INVALID_BLOCKS = 1000;
@@ -33,13 +31,14 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
33
31
  keyStore;
34
32
  epochCache;
35
33
  p2pClient;
36
- blockProposalHandler;
34
+ proposalHandler;
37
35
  blockSource;
38
36
  checkpointsBuilder;
39
37
  worldState;
40
38
  l1ToL2MessageSource;
41
39
  config;
42
40
  blobClient;
41
+ slashingProtectionSigner;
43
42
  dateProvider;
44
43
  tracer;
45
44
  validationService;
@@ -47,17 +46,15 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
47
46
  log;
48
47
  // Whether it has already registered handlers on the p2p client
49
48
  hasRegisteredHandlers;
50
- // Used to check if we are sending the same proposal twice
51
- previousProposal;
49
+ /** Tracks the last block proposal we created, to detect duplicate proposal attempts. */ lastProposedBlock;
50
+ /** Tracks the last checkpoint proposal we created. */ lastProposedCheckpoint;
52
51
  lastEpochForCommitteeUpdateLoop;
53
52
  epochCacheUpdateLoop;
53
+ /** Tracks the last epoch in which each attester successfully submitted at least one attestation. */ lastAttestedEpochByAttester;
54
54
  proposersOfInvalidBlocks;
55
- // TODO(palla/mbps): Remove this once checkpoint validation is stable and we can validate all blocks properly.
56
- // Tracks slots for which we have successfully validated a block proposal, so we can attest to checkpoint proposals for those slots.
57
- // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
58
- validatedBlockSlots;
59
- constructor(keyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
60
- super(), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.blockProposalHandler = blockProposalHandler, this.blockSource = blockSource, this.checkpointsBuilder = checkpointsBuilder, this.worldState = worldState, this.l1ToL2MessageSource = l1ToL2MessageSource, this.config = config, this.blobClient = blobClient, this.dateProvider = dateProvider, this.hasRegisteredHandlers = false, this.proposersOfInvalidBlocks = new Set(), this.validatedBlockSlots = new Set();
55
+ /** Tracks the last checkpoint proposal we attested to, to prevent equivocation. */ lastAttestedProposal;
56
+ constructor(keyStore, epochCache, p2pClient, proposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, slashingProtectionSigner, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
57
+ super(), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.proposalHandler = proposalHandler, this.blockSource = blockSource, this.checkpointsBuilder = checkpointsBuilder, this.worldState = worldState, this.l1ToL2MessageSource = l1ToL2MessageSource, this.config = config, this.blobClient = blobClient, this.slashingProtectionSigner = slashingProtectionSigner, this.dateProvider = dateProvider, this.hasRegisteredHandlers = false, this.lastAttestedEpochByAttester = new Map(), this.proposersOfInvalidBlocks = new Set();
61
58
  // Create child logger with fisherman prefix if in fisherman mode
62
59
  this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
63
60
  this.tracer = telemetry.getTracer('Validator');
@@ -96,6 +93,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
96
93
  this.log.trace(`No committee found for slot`);
97
94
  return;
98
95
  }
96
+ this.metrics.setCurrentEpoch(epoch);
99
97
  if (epoch !== this.lastEpochForCommitteeUpdateLoop) {
100
98
  const me = this.getValidatorAddresses();
101
99
  const committeeSet = new Set(committee.map((v)=>v.toString()));
@@ -111,30 +109,49 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
111
109
  this.log.error(`Error updating epoch committee`, err);
112
110
  }
113
111
  }
114
- static async new(config, checkpointsBuilder, worldState, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient()) {
112
+ static async new(config, checkpointsBuilder, worldState, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), slashingProtectionDb) {
115
113
  const metrics = new ValidatorMetrics(telemetry);
116
114
  const blockProposalValidator = new BlockProposalValidator(epochCache, {
117
- txsPermitted: !config.disableTransactions
115
+ txsPermitted: !config.disableTransactions,
116
+ maxTxsPerBlock: config.validateMaxTxsPerBlock
118
117
  });
119
- const blockProposalHandler = new BlockProposalHandler(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, epochCache, config, metrics, dateProvider, telemetry);
120
- let validatorKeyStore = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
121
- if (config.haSigningEnabled) {
118
+ const proposalHandler = new ProposalHandler(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, epochCache, config, blobClient, metrics, dateProvider, telemetry, undefined);
119
+ const nodeKeystoreAdapter = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
120
+ let slashingProtectionSigner;
121
+ if (slashingProtectionDb) {
122
+ // Shared database mode: use a pre-existing database (e.g. for testing HA setups).
123
+ ({ signer: slashingProtectionSigner } = createSignerFromSharedDb(slashingProtectionDb, config, {
124
+ telemetryClient: telemetry,
125
+ dateProvider
126
+ }));
127
+ } else if (config.haSigningEnabled) {
128
+ // Multi-node HA mode: use PostgreSQL-backed distributed locking.
122
129
  // If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
123
130
  const haConfig = {
124
131
  ...config,
125
132
  maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000
126
133
  };
127
- const { signer } = await createHASigner(haConfig);
128
- validatorKeyStore = new HAKeyStore(validatorKeyStore, signer);
134
+ ({ signer: slashingProtectionSigner } = await createHASigner(haConfig, {
135
+ telemetryClient: telemetry,
136
+ dateProvider
137
+ }));
138
+ } else {
139
+ // Single-node mode: use LMDB-backed local signing protection.
140
+ // This prevents double-signing if the node crashes and restarts mid-proposal.
141
+ ({ signer: slashingProtectionSigner } = await createLocalSignerWithProtection(config, {
142
+ telemetryClient: telemetry,
143
+ dateProvider
144
+ }));
129
145
  }
130
- const validator = new ValidatorClient(validatorKeyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, dateProvider, telemetry);
146
+ const validatorKeyStore = new HAKeyStore(nodeKeystoreAdapter, slashingProtectionSigner);
147
+ const validator = new ValidatorClient(validatorKeyStore, epochCache, p2pClient, proposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, slashingProtectionSigner, dateProvider, telemetry);
131
148
  return validator;
132
149
  }
133
150
  getValidatorAddresses() {
134
151
  return this.keyStore.getAddresses().filter((addr)=>!this.config.disabledValidators.some((disabled)=>disabled.equals(addr)));
135
152
  }
136
- getBlockProposalHandler() {
137
- return this.blockProposalHandler;
153
+ getProposalHandler() {
154
+ return this.proposalHandler;
138
155
  }
139
156
  signWithAddress(addr, msg, context) {
140
157
  return this.keyStore.signTypedDataWithAddress(addr, msg, context);
@@ -154,6 +171,11 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
154
171
  ...config
155
172
  };
156
173
  }
174
+ reloadKeystore(newManager) {
175
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
176
+ this.keyStore = new HAKeyStore(newAdapter, this.slashingProtectionSigner);
177
+ this.validationService = new ValidationService(this.keyStore, this.log.createChild('validation-service'));
178
+ }
157
179
  async start() {
158
180
  if (this.epochCacheUpdateLoop.isRunning()) {
159
181
  this.log.warn(`Validator client already started`);
@@ -185,7 +207,15 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
185
207
  // The checkpoint is received as CheckpointProposalCore since the lastBlock is extracted
186
208
  // and processed separately via the block handler above.
187
209
  const checkpointHandler = (checkpoint, proposalSender)=>this.attestToCheckpointProposal(checkpoint, proposalSender);
188
- this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
210
+ this.p2pClient.registerValidatorCheckpointProposalHandler(checkpointHandler);
211
+ // Duplicate proposal handler - triggers slashing for equivocation
212
+ this.p2pClient.registerDuplicateProposalCallback((info)=>{
213
+ this.handleDuplicateProposal(info);
214
+ });
215
+ // Duplicate attestation handler - triggers slashing for attestation equivocation
216
+ this.p2pClient.registerDuplicateAttestationCallback((info)=>{
217
+ this.handleDuplicateAttestation(info);
218
+ });
189
219
  const myAddresses = this.getValidatorAddresses();
190
220
  this.p2pClient.registerThisValidatorAddresses(myAddresses);
191
221
  await this.p2pClient.addReqRespSubProtocol(ReqRespSubProtocol.AUTH, this.handleAuthRequest.bind(this));
@@ -206,6 +236,13 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
206
236
  this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
207
237
  return false;
208
238
  }
239
+ // Log self-proposals from HA peers (same validator key on different nodes)
240
+ if (this.getValidatorAddresses().some((addr)=>addr.equals(proposer))) {
241
+ this.log.verbose(`Processing block proposal from HA peer for slot ${slotNumber}`, {
242
+ proposer: proposer.toString(),
243
+ slotNumber
244
+ });
245
+ }
209
246
  // Check if we're in the committee (for metrics purposes)
210
247
  const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
211
248
  const partOfCommittee = inCommittee.length > 0;
@@ -220,12 +257,12 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
220
257
  });
221
258
  // Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
222
259
  // In fisherman mode, we always reexecute to validate proposals.
223
- const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } = this.config;
224
- const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals || this.blobClient.canUpload();
225
- const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute && !escapeHatchOpen);
260
+ const { slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } = this.config;
261
+ const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n || partOfCommittee || alwaysReexecuteBlockProposals || this.blobClient.canUpload();
262
+ const validationResult = await this.proposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute && !escapeHatchOpen);
226
263
  if (!validationResult.isValid) {
227
- this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
228
264
  const reason = validationResult.reason || 'unknown';
265
+ this.log.warn(`Block proposal validation failed: ${reason}`, proposalInfo);
229
266
  // Classify failure reason: bad proposal vs node issue
230
267
  const badProposalReasons = [
231
268
  'invalid_proposal',
@@ -257,9 +294,6 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
257
294
  this.log.warn(`Escape hatch open for slot ${slotNumber}, rejecting block proposal`, proposalInfo);
258
295
  return false;
259
296
  }
260
- // TODO(palla/mbps): Remove this once checkpoint validation is stable.
261
- // Track that we successfully validated a block for this slot, so we can attest to checkpoint proposals for it.
262
- this.validatedBlockSlots.add(slotNumber);
263
297
  return true;
264
298
  }
265
299
  /**
@@ -268,52 +302,46 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
268
302
  * the lastBlock is extracted and processed separately via the block handler.
269
303
  * @returns Checkpoint attestations if valid, undefined otherwise
270
304
  */ async attestToCheckpointProposal(proposal, _proposalSender) {
271
- const slotNumber = proposal.slotNumber;
305
+ const proposalSlotNumber = proposal.slotNumber;
272
306
  const proposer = proposal.getSender();
273
307
  // If escape hatch is open for this slot's epoch, do not attest.
274
- if (await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber)) {
275
- this.log.warn(`Escape hatch open for slot ${slotNumber}, skipping checkpoint attestation handling`);
308
+ if (await this.epochCache.isEscapeHatchOpenAtSlot(proposalSlotNumber)) {
309
+ this.log.warn(`Escape hatch open for slot ${proposalSlotNumber}, skipping checkpoint attestation handling`);
276
310
  return undefined;
277
311
  }
278
- // Reject proposals with invalid signatures
279
- if (!proposer) {
280
- this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
312
+ // Ignore proposals from ourselves (may happen in HA setups)
313
+ if (proposer && this.getValidatorAddresses().some((addr)=>addr.equals(proposer))) {
314
+ this.log.debug(`Ignoring block proposal from self for slot ${proposalSlotNumber}`, {
315
+ proposer: proposer.toString(),
316
+ proposalSlotNumber
317
+ });
281
318
  return undefined;
282
319
  }
283
- // Check that I have any address in current committee before attesting
284
- const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
320
+ // Check that I have any address in the committee where this checkpoint will land before attesting
321
+ const inCommittee = await this.epochCache.filterInCommittee(proposalSlotNumber, this.getValidatorAddresses());
285
322
  const partOfCommittee = inCommittee.length > 0;
286
323
  const proposalInfo = {
287
- slotNumber,
324
+ proposalSlotNumber,
288
325
  archive: proposal.archive.toString(),
289
- proposer: proposer.toString(),
290
- txCount: proposal.txHashes.length
326
+ proposer: proposer?.toString()
291
327
  };
292
- this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
328
+ this.log.info(`Received checkpoint proposal for slot ${proposalSlotNumber}`, {
293
329
  ...proposalInfo,
294
- txHashes: proposal.txHashes.map((t)=>t.toString()),
295
330
  fishermanMode: this.config.fishermanMode || false
296
331
  });
297
- // TODO(palla/mbps): Remove this once checkpoint validation is stable.
298
- // Check that we have successfully validated a block for this slot before attesting to the checkpoint.
299
- if (!this.validatedBlockSlots.has(slotNumber)) {
300
- this.log.warn(`No validated block found for slot ${slotNumber}, refusing to attest to checkpoint`, proposalInfo);
301
- return undefined;
302
- }
303
- // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
304
- // TODO(palla/mbps): Change default to false once checkpoint validation is stable.
305
- if (this.config.skipCheckpointProposalValidation !== false) {
306
- this.log.verbose(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
332
+ // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set).
333
+ // Uses the cached result from the all-nodes callback if available (avoids double validation).
334
+ let checkpointNumber;
335
+ if (this.config.skipCheckpointProposalValidation) {
336
+ this.log.warn(`Skipping checkpoint proposal validation for slot ${proposalSlotNumber}`, proposalInfo);
337
+ checkpointNumber = CheckpointNumber(0);
307
338
  } else {
308
- const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
339
+ const validationResult = await this.proposalHandler.handleCheckpointProposal(proposal, proposalInfo);
309
340
  if (!validationResult.isValid) {
310
341
  this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
311
342
  return undefined;
312
343
  }
313
- }
314
- // Upload blobs to filestore if we can (fire and forget)
315
- if (this.blobClient.canUpload()) {
316
- void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
344
+ checkpointNumber = validationResult.checkpointNumber;
317
345
  }
318
346
  // Check that I have any address in current committee before attesting
319
347
  // In fisherman mode, we still create attestations for validation even if not in committee
@@ -322,12 +350,22 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
322
350
  return undefined;
323
351
  }
324
352
  // Provided all of the above checks pass, we can attest to the proposal
325
- this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
353
+ this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${proposalSlotNumber}`, {
326
354
  ...proposalInfo,
327
355
  inCommittee: partOfCommittee,
328
356
  fishermanMode: this.config.fishermanMode || false
329
357
  });
330
358
  this.metrics.incSuccessfulAttestations(inCommittee.length);
359
+ // Track epoch participation per attester: count each (attester, epoch) pair at most once
360
+ const proposalEpoch = getEpochAtSlot(proposalSlotNumber, this.epochCache.getL1Constants());
361
+ for (const attester of inCommittee){
362
+ const key = attester.toString();
363
+ const lastEpoch = this.lastAttestedEpochByAttester.get(key);
364
+ if (lastEpoch === undefined || proposalEpoch > lastEpoch) {
365
+ this.lastAttestedEpochByAttester.set(key, proposalEpoch);
366
+ this.metrics.incAttestedEpochCount(attester);
367
+ }
368
+ }
331
369
  // Determine which validators should attest
332
370
  let attestors;
333
371
  if (partOfCommittee) {
@@ -344,144 +382,39 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
344
382
  }
345
383
  if (this.config.fishermanMode) {
346
384
  // bail out early and don't save attestations to the pool in fisherman mode
347
- this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
385
+ this.log.info(`Creating checkpoint attestations for slot ${proposalSlotNumber}`, {
348
386
  ...proposalInfo,
349
387
  attestors: attestors.map((a)=>a.toString())
350
388
  });
351
389
  return undefined;
352
390
  }
353
- return this.createCheckpointAttestationsFromProposal(proposal, attestors);
354
- }
355
- async createCheckpointAttestationsFromProposal(proposal, attestors = []) {
356
- const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
357
- await this.p2pClient.addCheckpointAttestations(attestations);
358
- return attestations;
391
+ return await this.createCheckpointAttestationsFromProposal(proposal, attestors, checkpointNumber);
359
392
  }
360
393
  /**
361
- * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
362
- * @returns Validation result with isValid flag and reason if invalid.
363
- */ async validateCheckpointProposal(proposal, proposalInfo) {
364
- const slot = proposal.slotNumber;
365
- const timeoutSeconds = 10;
366
- // Wait for last block to sync by archive
367
- let lastBlockHeader;
368
- try {
369
- lastBlockHeader = await retryUntil(async ()=>{
370
- await this.blockSource.syncImmediate();
371
- return this.blockSource.getBlockHeaderByArchive(proposal.archive);
372
- }, `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`, timeoutSeconds, 0.5);
373
- } catch (err) {
374
- if (err instanceof TimeoutError) {
375
- this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
376
- return {
377
- isValid: false,
378
- reason: 'last_block_not_found'
379
- };
380
- }
381
- this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
382
- return {
383
- isValid: false,
384
- reason: 'block_fetch_error'
385
- };
386
- }
387
- if (!lastBlockHeader) {
388
- this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
389
- return {
390
- isValid: false,
391
- reason: 'last_block_not_found'
392
- };
393
- }
394
- // Get all full blocks for the slot and checkpoint
395
- const blocks = await this.blockSource.getBlocksForSlot(slot);
396
- if (blocks.length === 0) {
397
- this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
398
- return {
399
- isValid: false,
400
- reason: 'no_blocks_for_slot'
401
- };
394
+ * Checks if we should attest to a slot based on equivocation prevention rules.
395
+ * @returns true if we should attest, false if we should skip
396
+ */ shouldAttestToSlot(slotNumber) {
397
+ // If attestToEquivocatedProposals is true, always allow
398
+ if (this.config.attestToEquivocatedProposals) {
399
+ return true;
402
400
  }
403
- this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
404
- ...proposalInfo,
405
- blockNumbers: blocks.map((b)=>b.number)
406
- });
407
- // Get checkpoint constants from first block
408
- const firstBlock = blocks[0];
409
- const constants = this.extractCheckpointConstants(firstBlock);
410
- const checkpointNumber = firstBlock.checkpointNumber;
411
- // Get L1-to-L2 messages for this checkpoint
412
- const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
413
- // Compute the previous checkpoint out hashes for the epoch.
414
- // TODO: There can be a more efficient way to get the previous checkpoint out hashes without having to fetch the
415
- // actual checkpoints and the blocks/txs in them.
416
- const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
417
- const previousCheckpoints = (await this.blockSource.getCheckpointsForEpoch(epoch)).filter((b)=>b.number < checkpointNumber).sort((a, b)=>a.number - b.number);
418
- const previousCheckpointOutHashes = previousCheckpoints.map((c)=>c.getCheckpointOutHash());
419
- // Fork world state at the block before the first block
420
- const parentBlockNumber = BlockNumber(firstBlock.number - 1);
421
- const fork = await this.worldState.fork(parentBlockNumber);
422
- try {
423
- // Create checkpoint builder with all existing blocks
424
- const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(checkpointNumber, constants, l1ToL2Messages, previousCheckpointOutHashes, fork, blocks);
425
- // Complete the checkpoint to get computed values
426
- const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
427
- // Compare checkpoint header with proposal
428
- if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
429
- this.log.warn(`Checkpoint header mismatch`, {
430
- ...proposalInfo,
431
- computed: computedCheckpoint.header.toInspect(),
432
- proposal: proposal.checkpointHeader.toInspect()
433
- });
434
- return {
435
- isValid: false,
436
- reason: 'checkpoint_header_mismatch'
437
- };
438
- }
439
- // Compare archive root with proposal
440
- if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
441
- this.log.warn(`Archive root mismatch`, {
442
- ...proposalInfo,
443
- computed: computedCheckpoint.archive.root.toString(),
444
- proposal: proposal.archive.toString()
445
- });
446
- return {
447
- isValid: false,
448
- reason: 'archive_mismatch'
449
- };
450
- }
451
- // Check that the accumulated out hash matches the value in the proposal.
452
- const computedOutHash = computedCheckpoint.getCheckpointOutHash();
453
- const proposalOutHash = proposal.checkpointHeader.epochOutHash;
454
- if (!computedOutHash.equals(proposalOutHash)) {
455
- this.log.warn(`Epoch out hash mismatch`, {
456
- proposalOutHash: proposalOutHash.toString(),
457
- computedOutHash: computedOutHash.toString(),
458
- ...proposalInfo
459
- });
460
- return {
461
- isValid: false,
462
- reason: 'out_hash_mismatch'
463
- };
464
- }
465
- this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
466
- return {
467
- isValid: true
468
- };
469
- } finally{
470
- await fork.close();
401
+ // Check if incoming slot is strictly greater than last attested
402
+ if (this.lastAttestedProposal && slotNumber <= this.lastAttestedProposal.slotNumber) {
403
+ this.log.warn(`Refusing to process a proposal for slot ${slotNumber} given we already attested to a proposal for slot ${this.lastAttestedProposal.slotNumber}`);
404
+ return false;
471
405
  }
406
+ return true;
472
407
  }
473
- /**
474
- * Extract checkpoint global variables from a block.
475
- */ extractCheckpointConstants(block) {
476
- const gv = block.header.globalVariables;
477
- return {
478
- chainId: gv.chainId,
479
- version: gv.version,
480
- slotNumber: gv.slotNumber,
481
- coinbase: gv.coinbase,
482
- feeRecipient: gv.feeRecipient,
483
- gasFees: gv.gasFees
484
- };
408
+ async createCheckpointAttestationsFromProposal(proposal, attestors = [], checkpointNumber) {
409
+ // Equivocation check: must happen right before signing to minimize the race window
410
+ if (!this.shouldAttestToSlot(proposal.slotNumber)) {
411
+ return undefined;
412
+ }
413
+ const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors, checkpointNumber);
414
+ // Track the proposal we attested to (to prevent equivocation)
415
+ this.lastAttestedProposal = proposal;
416
+ await this.p2pClient.addOwnCheckpointAttestations(attestations);
417
+ return attestations;
485
418
  }
486
419
  /**
487
420
  * Uploads blobs for a checkpoint to the filestore (fire and forget).
@@ -498,7 +431,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
498
431
  return;
499
432
  }
500
433
  const blobFields = blocks.flatMap((b)=>b.toBlobFields());
501
- const blobs = getBlobsPerL1Block(blobFields);
434
+ const blobs = await getBlobsPerL1Block(blobFields);
502
435
  await this.blobClient.sendBlobsToFilestore(blobs);
503
436
  this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
504
437
  ...proposalInfo,
@@ -530,37 +463,92 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
530
463
  }
531
464
  ]);
532
465
  }
533
- async createBlockProposal(blockHeader, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, options) {
534
- // TODO(palla/mbps): Prevent double proposals properly
535
- // if (this.previousProposal?.slotNumber === blockHeader.globalVariables.slotNumber) {
536
- // this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
537
- // return Promise.resolve(undefined);
538
- // }
466
+ /**
467
+ * Handle detection of a duplicate proposal (equivocation).
468
+ * Emits a slash event when a proposer sends multiple proposals for the same position.
469
+ */ handleDuplicateProposal(info) {
470
+ const { slot, proposer, type } = info;
471
+ this.log.warn(`Triggering slash event for duplicate ${type} proposal from ${proposer.toString()} at slot ${slot}`, {
472
+ proposer: proposer.toString(),
473
+ slot,
474
+ type
475
+ });
476
+ // Emit slash event
477
+ this.emit(WANT_TO_SLASH_EVENT, [
478
+ {
479
+ validator: proposer,
480
+ amount: this.config.slashDuplicateProposalPenalty,
481
+ offenseType: OffenseType.DUPLICATE_PROPOSAL,
482
+ epochOrSlot: BigInt(slot)
483
+ }
484
+ ]);
485
+ }
486
+ /**
487
+ * Handle detection of a duplicate attestation (equivocation).
488
+ * Emits a slash event when an attester signs attestations for different proposals at the same slot.
489
+ */ handleDuplicateAttestation(info) {
490
+ const { slot, attester } = info;
491
+ this.log.warn(`Triggering slash event for duplicate attestation from ${attester.toString()} at slot ${slot}`, {
492
+ attester: attester.toString(),
493
+ slot
494
+ });
495
+ this.emit(WANT_TO_SLASH_EVENT, [
496
+ {
497
+ validator: attester,
498
+ amount: this.config.slashDuplicateAttestationPenalty,
499
+ offenseType: OffenseType.DUPLICATE_ATTESTATION,
500
+ epochOrSlot: BigInt(slot)
501
+ }
502
+ ]);
503
+ }
504
+ async createBlockProposal(blockHeader, checkpointNumber, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, options = {}) {
505
+ // Validate that we're not creating a proposal for an older or equal position
506
+ if (this.lastProposedBlock) {
507
+ const lastSlot = this.lastProposedBlock.slotNumber;
508
+ const lastIndex = this.lastProposedBlock.indexWithinCheckpoint;
509
+ const newSlot = blockHeader.globalVariables.slotNumber;
510
+ if (newSlot < lastSlot || newSlot === lastSlot && indexWithinCheckpoint <= lastIndex) {
511
+ throw new Error(`Cannot create block proposal for slot ${newSlot} index ${indexWithinCheckpoint}: ` + `already proposed block for slot ${lastSlot} index ${lastIndex}`);
512
+ }
513
+ }
539
514
  this.log.info(`Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`);
540
- const newProposal = await this.validationService.createBlockProposal(blockHeader, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, {
515
+ const newProposal = await this.validationService.createBlockProposal(blockHeader, checkpointNumber, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, {
541
516
  ...options,
542
517
  broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
543
518
  });
544
- this.previousProposal = newProposal;
519
+ this.lastProposedBlock = newProposal;
545
520
  return newProposal;
546
521
  }
547
- async createCheckpointProposal(checkpointHeader, archive, lastBlockInfo, proposerAddress, options) {
522
+ async createCheckpointProposal(checkpointHeader, archive, checkpointNumber, feeAssetPriceModifier, lastBlockProposal, proposerAddress, options = {}) {
523
+ // Validate that we're not creating a proposal for an older or equal slot
524
+ if (this.lastProposedCheckpoint) {
525
+ const lastSlot = this.lastProposedCheckpoint.slotNumber;
526
+ const newSlot = checkpointHeader.slotNumber;
527
+ if (newSlot <= lastSlot) {
528
+ throw new Error(`Cannot create checkpoint proposal for slot ${newSlot}: ` + `already proposed checkpoint for slot ${lastSlot}`);
529
+ }
530
+ }
548
531
  this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
549
- return await this.validationService.createCheckpointProposal(checkpointHeader, archive, lastBlockInfo, proposerAddress, options);
532
+ const newProposal = await this.validationService.createCheckpointProposal(checkpointHeader, archive, checkpointNumber, feeAssetPriceModifier, lastBlockProposal, proposerAddress, options);
533
+ this.lastProposedCheckpoint = newProposal;
534
+ return newProposal;
550
535
  }
551
536
  async broadcastBlockProposal(proposal) {
552
537
  await this.p2pClient.broadcastProposal(proposal);
553
538
  }
554
- async signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber) {
555
- return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber);
539
+ async signAttestationsAndSigners(attestationsAndSigners, proposer, slot, checkpointNumber) {
540
+ return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, checkpointNumber);
556
541
  }
557
- async collectOwnAttestations(proposal) {
542
+ async collectOwnAttestations(proposal, checkpointNumber) {
558
543
  const slot = proposal.slotNumber;
559
544
  const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
560
545
  this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, {
561
546
  inCommittee
562
547
  });
563
- const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
548
+ const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee, checkpointNumber);
549
+ if (!attestations) {
550
+ return [];
551
+ }
564
552
  // We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
565
553
  // other nodes can see that our validators did attest to this block proposal, and do not slash us
566
554
  // due to inactivity for missed attestations.
@@ -569,7 +557,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
569
557
  });
570
558
  return attestations;
571
559
  }
572
- async collectAttestations(proposal, required, deadline) {
560
+ async collectAttestations(proposal, required, deadline, checkpointNumber) {
573
561
  // Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
574
562
  const slot = proposal.slotNumber;
575
563
  this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
@@ -577,7 +565,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
577
565
  this.log.error(`Deadline ${deadline.toISOString()} for collecting ${required} attestations for slot ${slot} is in the past`);
578
566
  throw new AttestationTimeoutError(0, required, slot);
579
567
  }
580
- await this.collectOwnAttestations(proposal);
568
+ await this.collectOwnAttestations(proposal, checkpointNumber);
581
569
  const proposalId = proposal.archive.toString();
582
570
  const myAddresses = this.getValidatorAddresses();
583
571
  let attestations = [];