@aztec/validator-client 0.0.1-commit.9372f48 → 0.0.1-commit.949a33fd8

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