@aztec/validator-client 0.0.1-commit.e558bd1c → 0.0.1-commit.e5a3663dd

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