@aztec/validator-client 0.0.1-commit.1142ef1 → 0.0.1-commit.11bf3dd6e

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 (73) hide show
  1. package/README.md +92 -24
  2. package/dest/checkpoint_builder.d.ts +35 -26
  3. package/dest/checkpoint_builder.d.ts.map +1 -1
  4. package/dest/checkpoint_builder.js +144 -48
  5. package/dest/config.d.ts +1 -1
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +33 -19
  8. package/dest/duties/validation_service.d.ts +21 -10
  9. package/dest/duties/validation_service.d.ts.map +1 -1
  10. package/dest/duties/validation_service.js +67 -32
  11. package/dest/factory.d.ts +8 -5
  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 +99 -0
  18. package/dest/key_store/ha_key_store.d.ts.map +1 -0
  19. package/dest/key_store/ha_key_store.js +208 -0
  20. package/dest/key_store/index.d.ts +2 -1
  21. package/dest/key_store/index.d.ts.map +1 -1
  22. package/dest/key_store/index.js +1 -0
  23. package/dest/key_store/interface.d.ts +36 -6
  24. package/dest/key_store/interface.d.ts.map +1 -1
  25. package/dest/key_store/local_key_store.d.ts +10 -5
  26. package/dest/key_store/local_key_store.d.ts.map +1 -1
  27. package/dest/key_store/local_key_store.js +8 -4
  28. package/dest/key_store/node_keystore_adapter.d.ts +18 -5
  29. package/dest/key_store/node_keystore_adapter.d.ts.map +1 -1
  30. package/dest/key_store/node_keystore_adapter.js +18 -4
  31. package/dest/key_store/web3signer_key_store.d.ts +10 -5
  32. package/dest/key_store/web3signer_key_store.d.ts.map +1 -1
  33. package/dest/key_store/web3signer_key_store.js +8 -4
  34. package/dest/metrics.d.ts +16 -3
  35. package/dest/metrics.d.ts.map +1 -1
  36. package/dest/metrics.js +58 -5
  37. package/dest/proposal_handler.d.ts +108 -0
  38. package/dest/proposal_handler.d.ts.map +1 -0
  39. package/dest/proposal_handler.js +974 -0
  40. package/dest/validator.d.ts +47 -25
  41. package/dest/validator.d.ts.map +1 -1
  42. package/dest/validator.js +225 -229
  43. package/package.json +21 -17
  44. package/src/checkpoint_builder.ts +211 -61
  45. package/src/config.ts +32 -18
  46. package/src/duties/validation_service.ts +92 -36
  47. package/src/factory.ts +11 -3
  48. package/src/index.ts +1 -2
  49. package/src/key_store/ha_key_store.ts +269 -0
  50. package/src/key_store/index.ts +1 -0
  51. package/src/key_store/interface.ts +44 -5
  52. package/src/key_store/local_key_store.ts +13 -4
  53. package/src/key_store/node_keystore_adapter.ts +27 -4
  54. package/src/key_store/web3signer_key_store.ts +17 -4
  55. package/src/metrics.ts +81 -6
  56. package/src/proposal_handler.ts +1042 -0
  57. package/src/validator.ts +312 -265
  58. package/dest/block_proposal_handler.d.ts +0 -63
  59. package/dest/block_proposal_handler.d.ts.map +0 -1
  60. package/dest/block_proposal_handler.js +0 -551
  61. package/dest/tx_validator/index.d.ts +0 -3
  62. package/dest/tx_validator/index.d.ts.map +0 -1
  63. package/dest/tx_validator/index.js +0 -2
  64. package/dest/tx_validator/nullifier_cache.d.ts +0 -14
  65. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  66. package/dest/tx_validator/nullifier_cache.js +0 -24
  67. package/dest/tx_validator/tx_validator_factory.d.ts +0 -18
  68. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  69. package/dest/tx_validator/tx_validator_factory.js +0 -53
  70. package/src/block_proposal_handler.ts +0 -556
  71. package/src/tx_validator/index.ts +0 -2
  72. package/src/tx_validator/nullifier_cache.ts +0 -30
  73. package/src/tx_validator/tx_validator_factory.ts +0 -133
package/dest/validator.js CHANGED
@@ -1,20 +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';
9
+ import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
11
10
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
12
11
  import { getTelemetryClient } from '@aztec/telemetry-client';
12
+ import { createHASigner, createLocalSignerWithProtection, createSignerFromSharedDb } from '@aztec/validator-ha-signer/factory';
13
+ import { DutyType } from '@aztec/validator-ha-signer/types';
13
14
  import { EventEmitter } from 'events';
14
- import { BlockProposalHandler } from './block_proposal_handler.js';
15
15
  import { ValidationService } from './duties/validation_service.js';
16
+ import { HAKeyStore } from './key_store/ha_key_store.js';
16
17
  import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
17
18
  import { ValidatorMetrics } from './metrics.js';
19
+ import { ProposalHandler } from './proposal_handler.js';
18
20
  // We maintain a set of proposers who have proposed invalid blocks.
19
21
  // Just cap the set to avoid unbounded growth.
20
22
  const MAX_PROPOSERS_OF_INVALID_BLOCKS = 1000;
@@ -29,13 +31,14 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
29
31
  keyStore;
30
32
  epochCache;
31
33
  p2pClient;
32
- blockProposalHandler;
34
+ proposalHandler;
33
35
  blockSource;
34
36
  checkpointsBuilder;
35
37
  worldState;
36
38
  l1ToL2MessageSource;
37
39
  config;
38
40
  blobClient;
41
+ slashingProtectionSigner;
39
42
  dateProvider;
40
43
  tracer;
41
44
  validationService;
@@ -43,17 +46,15 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
43
46
  log;
44
47
  // Whether it has already registered handlers on the p2p client
45
48
  hasRegisteredHandlers;
46
- // Used to check if we are sending the same proposal twice
47
- previousProposal;
49
+ /** Tracks the last block proposal we created, to detect duplicate proposal attempts. */ lastProposedBlock;
50
+ /** Tracks the last checkpoint proposal we created. */ lastProposedCheckpoint;
48
51
  lastEpochForCommitteeUpdateLoop;
49
52
  epochCacheUpdateLoop;
53
+ /** Tracks the last epoch in which each attester successfully submitted at least one attestation. */ lastAttestedEpochByAttester;
50
54
  proposersOfInvalidBlocks;
51
- // TODO(palla/mbps): Remove this once checkpoint validation is stable and we can validate all blocks properly.
52
- // Tracks slots for which we have successfully validated a block proposal, so we can attest to checkpoint proposals for those slots.
53
- // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
54
- validatedBlockSlots;
55
- constructor(keyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
56
- 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();
57
58
  // Create child logger with fisherman prefix if in fisherman mode
58
59
  this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
59
60
  this.tracer = telemetry.getTracer('Validator');
@@ -92,6 +93,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
92
93
  this.log.trace(`No committee found for slot`);
93
94
  return;
94
95
  }
96
+ this.metrics.setCurrentEpoch(epoch);
95
97
  if (epoch !== this.lastEpochForCommitteeUpdateLoop) {
96
98
  const me = this.getValidatorAddresses();
97
99
  const committeeSet = new Set(committee.map((v)=>v.toString()));
@@ -107,23 +109,52 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
107
109
  this.log.error(`Error updating epoch committee`, err);
108
110
  }
109
111
  }
110
- static 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) {
111
113
  const metrics = new ValidatorMetrics(telemetry);
112
114
  const blockProposalValidator = new BlockProposalValidator(epochCache, {
113
- txsPermitted: !config.disableTransactions
115
+ txsPermitted: !config.disableTransactions,
116
+ maxTxsPerBlock: config.validateMaxTxsPerBlock
114
117
  });
115
- const blockProposalHandler = new BlockProposalHandler(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, config, metrics, dateProvider, telemetry);
116
- const validator = new ValidatorClient(NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager), epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, dateProvider, telemetry);
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.
129
+ // If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
130
+ const haConfig = {
131
+ ...config,
132
+ maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000
133
+ };
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
+ }));
145
+ }
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);
117
148
  return validator;
118
149
  }
119
150
  getValidatorAddresses() {
120
151
  return this.keyStore.getAddresses().filter((addr)=>!this.config.disabledValidators.some((disabled)=>disabled.equals(addr)));
121
152
  }
122
- getBlockProposalHandler() {
123
- return this.blockProposalHandler;
153
+ getProposalHandler() {
154
+ return this.proposalHandler;
124
155
  }
125
- signWithAddress(addr, msg) {
126
- return this.keyStore.signTypedDataWithAddress(addr, msg);
156
+ signWithAddress(addr, msg, context) {
157
+ return this.keyStore.signTypedDataWithAddress(addr, msg, context);
127
158
  }
128
159
  getCoinbaseForAttestor(attestor) {
129
160
  return this.keyStore.getCoinbaseAddress(attestor);
@@ -140,11 +171,17 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
140
171
  ...config
141
172
  };
142
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
+ }
143
179
  async start() {
144
180
  if (this.epochCacheUpdateLoop.isRunning()) {
145
181
  this.log.warn(`Validator client already started`);
146
182
  return;
147
183
  }
184
+ await this.keyStore.start();
148
185
  await this.registerHandlers();
149
186
  const myAddresses = this.getValidatorAddresses();
150
187
  const inCommittee = await this.epochCache.filterInCommittee('now', myAddresses);
@@ -157,6 +194,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
157
194
  }
158
195
  async stop() {
159
196
  await this.epochCacheUpdateLoop.stop();
197
+ await this.keyStore.stop();
160
198
  }
161
199
  /** Register handlers on the p2p client */ async registerHandlers() {
162
200
  if (!this.hasRegisteredHandlers) {
@@ -169,7 +207,15 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
169
207
  // The checkpoint is received as CheckpointProposalCore since the lastBlock is extracted
170
208
  // and processed separately via the block handler above.
171
209
  const checkpointHandler = (checkpoint, proposalSender)=>this.attestToCheckpointProposal(checkpoint, proposalSender);
172
- 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
+ });
173
219
  const myAddresses = this.getValidatorAddresses();
174
220
  this.p2pClient.registerThisValidatorAddresses(myAddresses);
175
221
  await this.p2pClient.addReqRespSubProtocol(ReqRespSubProtocol.AUTH, this.handleAuthRequest.bind(this));
@@ -181,12 +227,22 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
181
227
  * @returns true if the proposal is valid, false otherwise
182
228
  */ async validateBlockProposal(proposal, proposalSender) {
183
229
  const slotNumber = proposal.slotNumber;
230
+ // Note: During escape hatch, we still want to "validate" proposals for observability,
231
+ // but we intentionally reject them and disable slashing invalid block and attestation flow.
232
+ const escapeHatchOpen = await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber);
184
233
  const proposer = proposal.getSender();
185
234
  // Reject proposals with invalid signatures
186
235
  if (!proposer) {
187
236
  this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
188
237
  return false;
189
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
+ }
190
246
  // Check if we're in the committee (for metrics purposes)
191
247
  const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
192
248
  const partOfCommittee = inCommittee.length > 0;
@@ -201,12 +257,12 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
201
257
  });
202
258
  // Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
203
259
  // In fisherman mode, we always reexecute to validate proposals.
204
- const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } = this.config;
205
- const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals || this.blobClient.canUpload();
206
- const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute);
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);
207
263
  if (!validationResult.isValid) {
208
- this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
209
264
  const reason = validationResult.reason || 'unknown';
265
+ this.log.warn(`Block proposal validation failed: ${reason}`, proposalInfo);
210
266
  // Classify failure reason: bad proposal vs node issue
211
267
  const badProposalReasons = [
212
268
  'invalid_proposal',
@@ -222,7 +278,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
222
278
  this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
223
279
  }
224
280
  // Slash invalid block proposals (can happen even when not in committee)
225
- if (validationResult.reason && SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) && slashBroadcastedInvalidBlockPenalty > 0n) {
281
+ if (!escapeHatchOpen && validationResult.reason && SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) && slashBroadcastedInvalidBlockPenalty > 0n) {
226
282
  this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
227
283
  this.slashInvalidBlock(proposal);
228
284
  }
@@ -231,11 +287,13 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
231
287
  this.log.info(`Validated block proposal for slot ${slotNumber}`, {
232
288
  ...proposalInfo,
233
289
  inCommittee: partOfCommittee,
234
- fishermanMode: this.config.fishermanMode || false
290
+ fishermanMode: this.config.fishermanMode || false,
291
+ escapeHatchOpen
235
292
  });
236
- // TODO(palla/mbps): Remove this once checkpoint validation is stable.
237
- // Track that we successfully validated a block for this slot, so we can attest to checkpoint proposals for it.
238
- this.validatedBlockSlots.add(slotNumber);
293
+ if (escapeHatchOpen) {
294
+ this.log.warn(`Escape hatch open for slot ${slotNumber}, rejecting block proposal`, proposalInfo);
295
+ return false;
296
+ }
239
297
  return true;
240
298
  }
241
299
  /**
@@ -244,47 +302,46 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
244
302
  * the lastBlock is extracted and processed separately via the block handler.
245
303
  * @returns Checkpoint attestations if valid, undefined otherwise
246
304
  */ async attestToCheckpointProposal(proposal, _proposalSender) {
247
- const slotNumber = proposal.slotNumber;
305
+ const proposalSlotNumber = proposal.slotNumber;
248
306
  const proposer = proposal.getSender();
249
- // Reject proposals with invalid signatures
250
- if (!proposer) {
251
- this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
307
+ // If escape hatch is open for this slot's epoch, do not attest.
308
+ if (await this.epochCache.isEscapeHatchOpenAtSlot(proposalSlotNumber)) {
309
+ this.log.warn(`Escape hatch open for slot ${proposalSlotNumber}, skipping checkpoint attestation handling`);
252
310
  return undefined;
253
311
  }
254
- // Check that I have any address in current committee before attesting
255
- const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
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
+ });
318
+ return undefined;
319
+ }
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());
256
322
  const partOfCommittee = inCommittee.length > 0;
257
323
  const proposalInfo = {
258
- slotNumber,
324
+ proposalSlotNumber,
259
325
  archive: proposal.archive.toString(),
260
- proposer: proposer.toString(),
261
- txCount: proposal.txHashes.length
326
+ proposer: proposer?.toString()
262
327
  };
263
- this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
328
+ this.log.info(`Received checkpoint proposal for slot ${proposalSlotNumber}`, {
264
329
  ...proposalInfo,
265
- txHashes: proposal.txHashes.map((t)=>t.toString()),
266
330
  fishermanMode: this.config.fishermanMode || false
267
331
  });
268
- // TODO(palla/mbps): Remove this once checkpoint validation is stable.
269
- // Check that we have successfully validated a block for this slot before attesting to the checkpoint.
270
- if (!this.validatedBlockSlots.has(slotNumber)) {
271
- this.log.warn(`No validated block found for slot ${slotNumber}, refusing to attest to checkpoint`, proposalInfo);
272
- return undefined;
273
- }
274
- // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
275
- // TODO(palla/mbps): Change default to false once checkpoint validation is stable.
276
- if (this.config.skipCheckpointProposalValidation !== false) {
277
- 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);
278
338
  } else {
279
- const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
339
+ const validationResult = await this.proposalHandler.handleCheckpointProposal(proposal, proposalInfo);
280
340
  if (!validationResult.isValid) {
281
341
  this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
282
342
  return undefined;
283
343
  }
284
- }
285
- // Upload blobs to filestore if we can (fire and forget)
286
- if (this.blobClient.canUpload()) {
287
- void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
344
+ checkpointNumber = validationResult.checkpointNumber;
288
345
  }
289
346
  // Check that I have any address in current committee before attesting
290
347
  // In fisherman mode, we still create attestations for validation even if not in committee
@@ -293,12 +350,22 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
293
350
  return undefined;
294
351
  }
295
352
  // Provided all of the above checks pass, we can attest to the proposal
296
- 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}`, {
297
354
  ...proposalInfo,
298
355
  inCommittee: partOfCommittee,
299
356
  fishermanMode: this.config.fishermanMode || false
300
357
  });
301
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
+ }
302
369
  // Determine which validators should attest
303
370
  let attestors;
304
371
  if (partOfCommittee) {
@@ -315,163 +382,39 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
315
382
  }
316
383
  if (this.config.fishermanMode) {
317
384
  // bail out early and don't save attestations to the pool in fisherman mode
318
- this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
385
+ this.log.info(`Creating checkpoint attestations for slot ${proposalSlotNumber}`, {
319
386
  ...proposalInfo,
320
387
  attestors: attestors.map((a)=>a.toString())
321
388
  });
322
389
  return undefined;
323
390
  }
324
- return this.createCheckpointAttestationsFromProposal(proposal, attestors);
325
- }
326
- async createCheckpointAttestationsFromProposal(proposal, attestors = []) {
327
- const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
328
- await this.p2pClient.addCheckpointAttestations(attestations);
329
- return attestations;
391
+ return await this.createCheckpointAttestationsFromProposal(proposal, attestors, checkpointNumber);
330
392
  }
331
393
  /**
332
- * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
333
- * @returns Validation result with isValid flag and reason if invalid.
334
- */ async validateCheckpointProposal(proposal, proposalInfo) {
335
- const slot = proposal.slotNumber;
336
- const timeoutSeconds = 10;
337
- // Wait for last block to sync by archive
338
- let lastBlockHeader;
339
- try {
340
- lastBlockHeader = await retryUntil(async ()=>{
341
- await this.blockSource.syncImmediate();
342
- return this.blockSource.getBlockHeaderByArchive(proposal.archive);
343
- }, `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`, timeoutSeconds, 0.5);
344
- } catch (err) {
345
- if (err instanceof TimeoutError) {
346
- this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
347
- return {
348
- isValid: false,
349
- reason: 'last_block_not_found'
350
- };
351
- }
352
- this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
353
- return {
354
- isValid: false,
355
- reason: 'block_fetch_error'
356
- };
357
- }
358
- if (!lastBlockHeader) {
359
- this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
360
- return {
361
- isValid: false,
362
- reason: 'last_block_not_found'
363
- };
364
- }
365
- // Get the last full block to determine checkpoint number
366
- const lastBlock = await this.blockSource.getL2BlockNew(lastBlockHeader.getBlockNumber());
367
- if (!lastBlock) {
368
- this.log.warn(`Last block ${lastBlockHeader.getBlockNumber()} not found`, proposalInfo);
369
- return {
370
- isValid: false,
371
- reason: 'last_block_not_found'
372
- };
373
- }
374
- const checkpointNumber = lastBlock.checkpointNumber;
375
- // Get all full blocks for the slot and checkpoint
376
- const blocks = await this.getBlocksForSlot(slot, lastBlockHeader, checkpointNumber);
377
- if (blocks.length === 0) {
378
- this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
379
- return {
380
- isValid: false,
381
- reason: 'no_blocks_for_slot'
382
- };
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;
383
400
  }
384
- this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
385
- ...proposalInfo,
386
- blockNumbers: blocks.map((b)=>b.number)
387
- });
388
- // Get checkpoint constants from first block
389
- const firstBlock = blocks[0];
390
- const constants = this.extractCheckpointConstants(firstBlock);
391
- // Get L1-to-L2 messages for this checkpoint
392
- const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
393
- // Fork world state at the block before the first block
394
- const parentBlockNumber = BlockNumber(firstBlock.number - 1);
395
- const fork = await this.worldState.fork(parentBlockNumber);
396
- try {
397
- // Create checkpoint builder with all existing blocks
398
- const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(checkpointNumber, constants, l1ToL2Messages, fork, blocks);
399
- // Complete the checkpoint to get computed values
400
- const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
401
- // Compare checkpoint header with proposal
402
- if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
403
- this.log.warn(`Checkpoint header mismatch`, {
404
- ...proposalInfo,
405
- computed: computedCheckpoint.header.toInspect(),
406
- proposal: proposal.checkpointHeader.toInspect()
407
- });
408
- return {
409
- isValid: false,
410
- reason: 'checkpoint_header_mismatch'
411
- };
412
- }
413
- // Compare archive root with proposal
414
- if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
415
- this.log.warn(`Archive root mismatch`, {
416
- ...proposalInfo,
417
- computed: computedCheckpoint.archive.root.toString(),
418
- proposal: proposal.archive.toString()
419
- });
420
- return {
421
- isValid: false,
422
- reason: 'archive_mismatch'
423
- };
424
- }
425
- this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
426
- return {
427
- isValid: true
428
- };
429
- } finally{
430
- 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;
431
405
  }
406
+ return true;
432
407
  }
433
- /**
434
- * Get all full blocks for a given slot and checkpoint by walking backwards from the last block.
435
- * Returns blocks in ascending order (earliest to latest).
436
- * TODO(palla/mbps): Add getL2BlocksForSlot() to L2BlockSource interface for efficiency.
437
- */ async getBlocksForSlot(slot, lastBlockHeader, checkpointNumber) {
438
- const blocks = [];
439
- let currentHeader = lastBlockHeader;
440
- const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
441
- while(currentHeader.getSlot() === slot){
442
- const block = await this.blockSource.getL2BlockNew(currentHeader.getBlockNumber());
443
- if (!block) {
444
- this.log.warn(`Block ${currentHeader.getBlockNumber()} not found while getting blocks for slot ${slot}`);
445
- break;
446
- }
447
- if (block.checkpointNumber !== checkpointNumber) {
448
- break;
449
- }
450
- blocks.unshift(block);
451
- const prevArchive = currentHeader.lastArchive.root;
452
- if (prevArchive.equals(genesisArchiveRoot)) {
453
- break;
454
- }
455
- const prevHeader = await this.blockSource.getBlockHeaderByArchive(prevArchive);
456
- if (!prevHeader || prevHeader.getSlot() !== slot) {
457
- break;
458
- }
459
- currentHeader = prevHeader;
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;
460
412
  }
461
- return blocks;
462
- }
463
- /**
464
- * Extract checkpoint global variables from a block.
465
- */ extractCheckpointConstants(block) {
466
- const gv = block.header.globalVariables;
467
- return {
468
- chainId: gv.chainId,
469
- version: gv.version,
470
- slotNumber: gv.slotNumber,
471
- coinbase: gv.coinbase,
472
- feeRecipient: gv.feeRecipient,
473
- gasFees: gv.gasFees
474
- };
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;
475
418
  }
476
419
  /**
477
420
  * Uploads blobs for a checkpoint to the filestore (fire and forget).
@@ -482,19 +425,13 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
482
425
  this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
483
426
  return;
484
427
  }
485
- // Get the last full block to determine checkpoint number
486
- const lastBlock = await this.blockSource.getL2BlockNew(lastBlockHeader.getBlockNumber());
487
- if (!lastBlock) {
488
- this.log.warn(`Failed to get last block for blob upload`, proposalInfo);
489
- return;
490
- }
491
- const blocks = await this.getBlocksForSlot(proposal.slotNumber, lastBlockHeader, lastBlock.checkpointNumber);
428
+ const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
492
429
  if (blocks.length === 0) {
493
430
  this.log.warn(`No blocks found for blob upload`, proposalInfo);
494
431
  return;
495
432
  }
496
433
  const blobFields = blocks.flatMap((b)=>b.toBlobFields());
497
- const blobs = getBlobsPerL1Block(blobFields);
434
+ const blobs = await getBlobsPerL1Block(blobFields);
498
435
  await this.blobClient.sendBlobsToFilestore(blobs);
499
436
  this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
500
437
  ...proposalInfo,
@@ -526,37 +463,92 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
526
463
  }
527
464
  ]);
528
465
  }
529
- async createBlockProposal(blockHeader, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, options) {
530
- // TODO(palla/mbps): Prevent double proposals properly
531
- // if (this.previousProposal?.slotNumber === blockHeader.globalVariables.slotNumber) {
532
- // this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
533
- // return Promise.resolve(undefined);
534
- // }
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
+ }
535
514
  this.log.info(`Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`);
536
- 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, {
537
516
  ...options,
538
517
  broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
539
518
  });
540
- this.previousProposal = newProposal;
519
+ this.lastProposedBlock = newProposal;
541
520
  return newProposal;
542
521
  }
543
- 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
+ }
544
531
  this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
545
- 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;
546
535
  }
547
536
  async broadcastBlockProposal(proposal) {
548
537
  await this.p2pClient.broadcastProposal(proposal);
549
538
  }
550
- async signAttestationsAndSigners(attestationsAndSigners, proposer) {
551
- return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
539
+ async signAttestationsAndSigners(attestationsAndSigners, proposer, slot, checkpointNumber) {
540
+ return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, checkpointNumber);
552
541
  }
553
- async collectOwnAttestations(proposal) {
542
+ async collectOwnAttestations(proposal, checkpointNumber) {
554
543
  const slot = proposal.slotNumber;
555
544
  const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
556
545
  this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, {
557
546
  inCommittee
558
547
  });
559
- const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
548
+ const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee, checkpointNumber);
549
+ if (!attestations) {
550
+ return [];
551
+ }
560
552
  // We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
561
553
  // other nodes can see that our validators did attest to this block proposal, and do not slash us
562
554
  // due to inactivity for missed attestations.
@@ -565,7 +557,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
565
557
  });
566
558
  return attestations;
567
559
  }
568
- async collectAttestations(proposal, required, deadline) {
560
+ async collectAttestations(proposal, required, deadline, checkpointNumber) {
569
561
  // Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
570
562
  const slot = proposal.slotNumber;
571
563
  this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
@@ -573,7 +565,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
573
565
  this.log.error(`Deadline ${deadline.toISOString()} for collecting ${required} attestations for slot ${slot} is in the past`);
574
566
  throw new AttestationTimeoutError(0, required, slot);
575
567
  }
576
- await this.collectOwnAttestations(proposal);
568
+ await this.collectOwnAttestations(proposal, checkpointNumber);
577
569
  const proposalId = proposal.archive.toString();
578
570
  const myAddresses = this.getValidatorAddresses();
579
571
  let attestations = [];
@@ -630,7 +622,11 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
630
622
  return Buffer.alloc(0);
631
623
  }
632
624
  const payloadToSign = authRequest.getPayloadToSign();
633
- const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign);
625
+ // AUTH_REQUEST doesn't require HA protection - multiple signatures are safe
626
+ const context = {
627
+ dutyType: DutyType.AUTH_REQUEST
628
+ };
629
+ const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign, context);
634
630
  const authResponse = new AuthResponse(statusMessage, signature);
635
631
  return authResponse.toBuffer();
636
632
  }