@aztec/validator-client 0.0.1-commit.b655e406 → 0.0.1-commit.c0b82b2

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 (57) hide show
  1. package/README.md +285 -0
  2. package/dest/block_proposal_handler.d.ts +25 -14
  3. package/dest/block_proposal_handler.d.ts.map +1 -1
  4. package/dest/block_proposal_handler.js +339 -93
  5. package/dest/checkpoint_builder.d.ts +69 -0
  6. package/dest/checkpoint_builder.d.ts.map +1 -0
  7. package/dest/checkpoint_builder.js +180 -0
  8. package/dest/config.d.ts +1 -1
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +20 -7
  11. package/dest/duties/validation_service.d.ts +42 -13
  12. package/dest/duties/validation_service.d.ts.map +1 -1
  13. package/dest/duties/validation_service.js +113 -31
  14. package/dest/factory.d.ts +13 -8
  15. package/dest/factory.d.ts.map +1 -1
  16. package/dest/factory.js +2 -2
  17. package/dest/index.d.ts +2 -1
  18. package/dest/index.d.ts.map +1 -1
  19. package/dest/index.js +1 -0
  20. package/dest/key_store/ha_key_store.d.ts +99 -0
  21. package/dest/key_store/ha_key_store.d.ts.map +1 -0
  22. package/dest/key_store/ha_key_store.js +208 -0
  23. package/dest/key_store/index.d.ts +2 -1
  24. package/dest/key_store/index.d.ts.map +1 -1
  25. package/dest/key_store/index.js +1 -0
  26. package/dest/key_store/interface.d.ts +36 -6
  27. package/dest/key_store/interface.d.ts.map +1 -1
  28. package/dest/key_store/local_key_store.d.ts +10 -5
  29. package/dest/key_store/local_key_store.d.ts.map +1 -1
  30. package/dest/key_store/local_key_store.js +9 -5
  31. package/dest/key_store/node_keystore_adapter.d.ts +18 -5
  32. package/dest/key_store/node_keystore_adapter.d.ts.map +1 -1
  33. package/dest/key_store/node_keystore_adapter.js +18 -4
  34. package/dest/key_store/web3signer_key_store.d.ts +10 -11
  35. package/dest/key_store/web3signer_key_store.d.ts.map +1 -1
  36. package/dest/key_store/web3signer_key_store.js +9 -5
  37. package/dest/metrics.d.ts +4 -3
  38. package/dest/metrics.d.ts.map +1 -1
  39. package/dest/metrics.js +34 -30
  40. package/dest/validator.d.ts +75 -22
  41. package/dest/validator.d.ts.map +1 -1
  42. package/dest/validator.js +498 -65
  43. package/package.json +24 -14
  44. package/src/block_proposal_handler.ts +265 -73
  45. package/src/checkpoint_builder.ts +328 -0
  46. package/src/config.ts +20 -6
  47. package/src/duties/validation_service.ts +164 -38
  48. package/src/factory.ts +17 -8
  49. package/src/index.ts +1 -0
  50. package/src/key_store/ha_key_store.ts +269 -0
  51. package/src/key_store/index.ts +1 -0
  52. package/src/key_store/interface.ts +44 -5
  53. package/src/key_store/local_key_store.ts +14 -5
  54. package/src/key_store/node_keystore_adapter.ts +28 -5
  55. package/src/key_store/web3signer_key_store.ts +18 -5
  56. package/src/metrics.ts +45 -33
  57. package/src/validator.ts +669 -97
package/dest/validator.js CHANGED
@@ -1,14 +1,24 @@
1
+ import { getBlobsPerL1Block } from '@aztec/blob-lib';
2
+ import { validateFeeAssetPriceModifier } from '@aztec/ethereum/contracts';
3
+ import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
4
+ import { TimeoutError } from '@aztec/foundation/error';
1
5
  import { createLogger } from '@aztec/foundation/log';
6
+ import { retryUntil } from '@aztec/foundation/retry';
2
7
  import { RunningPromise } from '@aztec/foundation/running-promise';
3
8
  import { sleep } from '@aztec/foundation/sleep';
4
9
  import { DateProvider } from '@aztec/foundation/timer';
5
10
  import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
6
11
  import { OffenseType, WANT_TO_SLASH_EVENT } from '@aztec/slasher';
12
+ import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
13
+ import { accumulateCheckpointOutHashes } from '@aztec/stdlib/messaging';
7
14
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
8
15
  import { getTelemetryClient } from '@aztec/telemetry-client';
16
+ import { createHASigner } from '@aztec/validator-ha-signer/factory';
17
+ import { DutyType } from '@aztec/validator-ha-signer/types';
9
18
  import { EventEmitter } from 'events';
10
19
  import { BlockProposalHandler } from './block_proposal_handler.js';
11
20
  import { ValidationService } from './duties/validation_service.js';
21
+ import { HAKeyStore } from './key_store/ha_key_store.js';
12
22
  import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
13
23
  import { ValidatorMetrics } from './metrics.js';
14
24
  // We maintain a set of proposers who have proposed invalid blocks.
@@ -26,26 +36,35 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
26
36
  epochCache;
27
37
  p2pClient;
28
38
  blockProposalHandler;
39
+ blockSource;
40
+ checkpointsBuilder;
41
+ worldState;
42
+ l1ToL2MessageSource;
29
43
  config;
44
+ blobClient;
45
+ haSigner;
30
46
  dateProvider;
31
- log;
32
47
  tracer;
33
48
  validationService;
34
49
  metrics;
50
+ log;
35
51
  // Whether it has already registered handlers on the p2p client
36
52
  hasRegisteredHandlers;
37
- // Used to check if we are sending the same proposal twice
38
- previousProposal;
53
+ /** Tracks the last block proposal we created, to detect duplicate proposal attempts. */ lastProposedBlock;
54
+ /** Tracks the last checkpoint proposal we created. */ lastProposedCheckpoint;
39
55
  lastEpochForCommitteeUpdateLoop;
40
56
  epochCacheUpdateLoop;
41
57
  proposersOfInvalidBlocks;
42
- constructor(keyStore, epochCache, p2pClient, blockProposalHandler, config, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
43
- super(), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.blockProposalHandler = blockProposalHandler, this.config = config, this.dateProvider = dateProvider, this.log = log, this.hasRegisteredHandlers = false, this.proposersOfInvalidBlocks = new Set();
58
+ /** Tracks the last checkpoint proposal we attested to, to prevent equivocation. */ lastAttestedProposal;
59
+ constructor(keyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, haSigner, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
60
+ super(), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.blockProposalHandler = blockProposalHandler, this.blockSource = blockSource, this.checkpointsBuilder = checkpointsBuilder, this.worldState = worldState, this.l1ToL2MessageSource = l1ToL2MessageSource, this.config = config, this.blobClient = blobClient, this.haSigner = haSigner, this.dateProvider = dateProvider, this.hasRegisteredHandlers = false, this.proposersOfInvalidBlocks = new Set();
61
+ // Create child logger with fisherman prefix if in fisherman mode
62
+ this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
44
63
  this.tracer = telemetry.getTracer('Validator');
45
64
  this.metrics = new ValidatorMetrics(telemetry);
46
- this.validationService = new ValidationService(keyStore, log.createChild('validation-service'));
65
+ this.validationService = new ValidationService(keyStore, this.log.createChild('validation-service'));
47
66
  // Refresh epoch cache every second to trigger alert if participation in committee changes
48
- this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), log, 1000);
67
+ this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), this.log, 1000);
49
68
  const myAddresses = this.getValidatorAddresses();
50
69
  this.log.verbose(`Initialized validator with addresses: ${myAddresses.map((a)=>a.toString()).join(', ')}`);
51
70
  }
@@ -92,13 +111,29 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
92
111
  this.log.error(`Error updating epoch committee`, err);
93
112
  }
94
113
  }
95
- static new(config, blockBuilder, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, dateProvider = new DateProvider(), telemetry = getTelemetryClient()) {
114
+ static async new(config, checkpointsBuilder, worldState, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient()) {
96
115
  const metrics = new ValidatorMetrics(telemetry);
97
116
  const blockProposalValidator = new BlockProposalValidator(epochCache, {
98
117
  txsPermitted: !config.disableTransactions
99
118
  });
100
- const blockProposalHandler = new BlockProposalHandler(blockBuilder, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, config, metrics, dateProvider, telemetry);
101
- const validator = new ValidatorClient(NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager), epochCache, p2pClient, blockProposalHandler, config, dateProvider, telemetry);
119
+ const blockProposalHandler = new BlockProposalHandler(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, epochCache, config, metrics, dateProvider, telemetry);
120
+ const nodeKeystoreAdapter = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
121
+ let validatorKeyStore = nodeKeystoreAdapter;
122
+ let haSigner;
123
+ if (config.haSigningEnabled) {
124
+ // If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
125
+ const haConfig = {
126
+ ...config,
127
+ maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000
128
+ };
129
+ const { signer } = await createHASigner(haConfig, {
130
+ telemetryClient: telemetry,
131
+ dateProvider
132
+ });
133
+ haSigner = signer;
134
+ validatorKeyStore = new HAKeyStore(nodeKeystoreAdapter, signer);
135
+ }
136
+ const validator = new ValidatorClient(validatorKeyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, haSigner, dateProvider, telemetry);
102
137
  return validator;
103
138
  }
104
139
  getValidatorAddresses() {
@@ -107,12 +142,8 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
107
142
  getBlockProposalHandler() {
108
143
  return this.blockProposalHandler;
109
144
  }
110
- // Proxy method for backwards compatibility with tests
111
- reExecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages) {
112
- return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
113
- }
114
- signWithAddress(addr, msg) {
115
- return this.keyStore.signTypedDataWithAddress(addr, msg);
145
+ signWithAddress(addr, msg, context) {
146
+ return this.keyStore.signTypedDataWithAddress(addr, msg, context);
116
147
  }
117
148
  getCoinbaseForAttestor(attestor) {
118
149
  return this.keyStore.getCoinbaseAddress(attestor);
@@ -129,62 +160,107 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
129
160
  ...config
130
161
  };
131
162
  }
163
+ reloadKeystore(newManager) {
164
+ if (this.config.haSigningEnabled && !this.haSigner) {
165
+ this.log.warn('HA signing is enabled in config but was not initialized at startup. ' + 'Restart the node to enable HA signing.');
166
+ } else if (!this.config.haSigningEnabled && this.haSigner) {
167
+ this.log.warn('HA signing was disabled via config update but the HA signer is still active. ' + 'Restart the node to fully disable HA signing.');
168
+ }
169
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
170
+ if (this.haSigner) {
171
+ this.keyStore = new HAKeyStore(newAdapter, this.haSigner);
172
+ } else {
173
+ this.keyStore = newAdapter;
174
+ }
175
+ this.validationService = new ValidationService(this.keyStore, this.log.createChild('validation-service'));
176
+ }
132
177
  async start() {
133
178
  if (this.epochCacheUpdateLoop.isRunning()) {
134
179
  this.log.warn(`Validator client already started`);
135
180
  return;
136
181
  }
182
+ await this.keyStore.start();
137
183
  await this.registerHandlers();
138
184
  const myAddresses = this.getValidatorAddresses();
139
185
  const inCommittee = await this.epochCache.filterInCommittee('now', myAddresses);
186
+ this.log.info(`Started validator with addresses: ${myAddresses.map((a)=>a.toString()).join(', ')}`);
140
187
  if (inCommittee.length > 0) {
141
- this.log.info(`Started validator with addresses in current validator committee: ${inCommittee.map((a)=>a.toString()).join(', ')}`);
142
- } else {
143
- this.log.info(`Started validator with addresses: ${myAddresses.map((a)=>a.toString()).join(', ')}`);
188
+ this.log.info(`Addresses in current validator committee: ${inCommittee.map((a)=>a.toString()).join(', ')}`);
144
189
  }
145
190
  this.epochCacheUpdateLoop.start();
146
191
  return Promise.resolve();
147
192
  }
148
193
  async stop() {
149
194
  await this.epochCacheUpdateLoop.stop();
195
+ await this.keyStore.stop();
150
196
  }
151
197
  /** Register handlers on the p2p client */ async registerHandlers() {
152
198
  if (!this.hasRegisteredHandlers) {
153
199
  this.hasRegisteredHandlers = true;
154
200
  this.log.debug(`Registering validator handlers for p2p client`);
155
- const handler = (block, proposalSender)=>this.attestToProposal(block, proposalSender);
156
- this.p2pClient.registerBlockProposalHandler(handler);
201
+ // Block proposal handler - validates but does NOT attest (validators only attest to checkpoints)
202
+ const blockHandler = (block, proposalSender)=>this.validateBlockProposal(block, proposalSender);
203
+ this.p2pClient.registerBlockProposalHandler(blockHandler);
204
+ // Checkpoint proposal handler - validates and creates attestations
205
+ // The checkpoint is received as CheckpointProposalCore since the lastBlock is extracted
206
+ // and processed separately via the block handler above.
207
+ const checkpointHandler = (checkpoint, proposalSender)=>this.attestToCheckpointProposal(checkpoint, proposalSender);
208
+ this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
209
+ // Duplicate proposal handler - triggers slashing for equivocation
210
+ this.p2pClient.registerDuplicateProposalCallback((info)=>{
211
+ this.handleDuplicateProposal(info);
212
+ });
213
+ // Duplicate attestation handler - triggers slashing for attestation equivocation
214
+ this.p2pClient.registerDuplicateAttestationCallback((info)=>{
215
+ this.handleDuplicateAttestation(info);
216
+ });
157
217
  const myAddresses = this.getValidatorAddresses();
158
218
  this.p2pClient.registerThisValidatorAddresses(myAddresses);
159
219
  await this.p2pClient.addReqRespSubProtocol(ReqRespSubProtocol.AUTH, this.handleAuthRequest.bind(this));
160
220
  }
161
221
  }
162
- async attestToProposal(proposal, proposalSender) {
163
- const slotNumber = proposal.slotNumber.toBigInt();
222
+ /**
223
+ * Validate a block proposal from a peer.
224
+ * Note: Validators do NOT attest to individual blocks - attestations are only for checkpoint proposals.
225
+ * @returns true if the proposal is valid, false otherwise
226
+ */ async validateBlockProposal(proposal, proposalSender) {
227
+ const slotNumber = proposal.slotNumber;
228
+ // Note: During escape hatch, we still want to "validate" proposals for observability,
229
+ // but we intentionally reject them and disable slashing invalid block and attestation flow.
230
+ const escapeHatchOpen = await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber);
164
231
  const proposer = proposal.getSender();
165
232
  // Reject proposals with invalid signatures
166
233
  if (!proposer) {
167
- this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
168
- return undefined;
234
+ this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
235
+ return false;
169
236
  }
170
- // Check that I have any address in current committee before attesting
237
+ // Ignore proposals from ourselves (may happen in HA setups)
238
+ if (this.getValidatorAddresses().some((addr)=>addr.equals(proposer))) {
239
+ this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
240
+ proposer: proposer.toString(),
241
+ slotNumber
242
+ });
243
+ return false;
244
+ }
245
+ // Check if we're in the committee (for metrics purposes)
171
246
  const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
172
247
  const partOfCommittee = inCommittee.length > 0;
173
248
  const proposalInfo = {
174
249
  ...proposal.toBlockInfo(),
175
250
  proposer: proposer.toString()
176
251
  };
177
- this.log.info(`Received proposal for slot ${slotNumber}`, {
252
+ this.log.info(`Received block proposal for slot ${slotNumber}`, {
178
253
  ...proposalInfo,
179
- txHashes: proposal.txHashes.map((t)=>t.toString())
254
+ txHashes: proposal.txHashes.map((t)=>t.toString()),
255
+ fishermanMode: this.config.fishermanMode || false
180
256
  });
181
- // Reexecute txs if we are part of the committee so we can attest, or if slashing is enabled so we can slash
182
- // invalid proposals even when not in the committee, or if we are configured to always reexecute for monitoring purposes.
183
- const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals } = this.config;
184
- const shouldReexecute = slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals;
185
- const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute);
257
+ // Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
258
+ // In fisherman mode, we always reexecute to validate proposals.
259
+ const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } = this.config;
260
+ const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals || this.blobClient.canUpload();
261
+ const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute && !escapeHatchOpen);
186
262
  if (!validationResult.isValid) {
187
- this.log.warn(`Proposal validation failed: ${validationResult.reason}`, proposalInfo);
263
+ this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
188
264
  const reason = validationResult.reason || 'unknown';
189
265
  // Classify failure reason: bad proposal vs node issue
190
266
  const badProposalReasons = [
@@ -197,26 +273,316 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
197
273
  if (badProposalReasons.includes(reason)) {
198
274
  this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
199
275
  } else {
200
- // Node issues so we can't attest
276
+ // Node issues so we can't validate
201
277
  this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
202
278
  }
203
279
  // Slash invalid block proposals (can happen even when not in committee)
204
- if (validationResult.reason && SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) && slashBroadcastedInvalidBlockPenalty > 0n) {
280
+ if (!escapeHatchOpen && validationResult.reason && SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) && slashBroadcastedInvalidBlockPenalty > 0n) {
205
281
  this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
206
282
  this.slashInvalidBlock(proposal);
207
283
  }
284
+ return false;
285
+ }
286
+ this.log.info(`Validated block proposal for slot ${slotNumber}`, {
287
+ ...proposalInfo,
288
+ inCommittee: partOfCommittee,
289
+ fishermanMode: this.config.fishermanMode || false,
290
+ escapeHatchOpen
291
+ });
292
+ if (escapeHatchOpen) {
293
+ this.log.warn(`Escape hatch open for slot ${slotNumber}, rejecting block proposal`, proposalInfo);
294
+ return false;
295
+ }
296
+ return true;
297
+ }
298
+ /**
299
+ * Validate and attest to a checkpoint proposal from a peer.
300
+ * The proposal is received as CheckpointProposalCore (without lastBlock) since
301
+ * the lastBlock is extracted and processed separately via the block handler.
302
+ * @returns Checkpoint attestations if valid, undefined otherwise
303
+ */ async attestToCheckpointProposal(proposal, _proposalSender) {
304
+ const slotNumber = proposal.slotNumber;
305
+ const proposer = proposal.getSender();
306
+ // If escape hatch is open for this slot's epoch, do not attest.
307
+ if (await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber)) {
308
+ this.log.warn(`Escape hatch open for slot ${slotNumber}, skipping checkpoint attestation handling`);
309
+ return undefined;
310
+ }
311
+ // Reject proposals with invalid signatures
312
+ if (!proposer) {
313
+ this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
314
+ return undefined;
315
+ }
316
+ // Ignore proposals from ourselves (may happen in HA setups)
317
+ if (this.getValidatorAddresses().some((addr)=>addr.equals(proposer))) {
318
+ this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
319
+ proposer: proposer.toString(),
320
+ slotNumber
321
+ });
322
+ return undefined;
323
+ }
324
+ // Validate fee asset price modifier is within allowed range
325
+ if (!validateFeeAssetPriceModifier(proposal.feeAssetPriceModifier)) {
326
+ this.log.warn(`Received checkpoint proposal with invalid feeAssetPriceModifier ${proposal.feeAssetPriceModifier} for slot ${slotNumber}`);
208
327
  return undefined;
209
328
  }
210
329
  // Check that I have any address in current committee before attesting
211
- if (!partOfCommittee) {
330
+ const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
331
+ const partOfCommittee = inCommittee.length > 0;
332
+ const proposalInfo = {
333
+ slotNumber,
334
+ archive: proposal.archive.toString(),
335
+ proposer: proposer.toString(),
336
+ txCount: proposal.txHashes.length
337
+ };
338
+ this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
339
+ ...proposalInfo,
340
+ txHashes: proposal.txHashes.map((t)=>t.toString()),
341
+ fishermanMode: this.config.fishermanMode || false
342
+ });
343
+ // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
344
+ if (this.config.skipCheckpointProposalValidation) {
345
+ this.log.warn(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
346
+ } else {
347
+ const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
348
+ if (!validationResult.isValid) {
349
+ this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
350
+ return undefined;
351
+ }
352
+ }
353
+ // Upload blobs to filestore if we can (fire and forget)
354
+ if (this.blobClient.canUpload()) {
355
+ void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
356
+ }
357
+ // Check that I have any address in current committee before attesting
358
+ // In fisherman mode, we still create attestations for validation even if not in committee
359
+ if (!partOfCommittee && !this.config.fishermanMode) {
212
360
  this.log.verbose(`No validator in the current committee, skipping attestation`, proposalInfo);
213
361
  return undefined;
214
362
  }
215
363
  // Provided all of the above checks pass, we can attest to the proposal
216
- this.log.info(`Attesting to proposal for slot ${slotNumber}`, proposalInfo);
364
+ this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
365
+ ...proposalInfo,
366
+ inCommittee: partOfCommittee,
367
+ fishermanMode: this.config.fishermanMode || false
368
+ });
217
369
  this.metrics.incSuccessfulAttestations(inCommittee.length);
218
- // If the above function does not throw an error, then we can attest to the proposal
219
- return this.createBlockAttestationsFromProposal(proposal, inCommittee);
370
+ // Determine which validators should attest
371
+ let attestors;
372
+ if (partOfCommittee) {
373
+ attestors = inCommittee;
374
+ } else if (this.config.fishermanMode) {
375
+ // In fisherman mode, create attestations for validation purposes even if not in committee. These won't be broadcast.
376
+ attestors = this.getValidatorAddresses();
377
+ } else {
378
+ attestors = [];
379
+ }
380
+ // Only create attestations if we have attestors
381
+ if (attestors.length === 0) {
382
+ return undefined;
383
+ }
384
+ if (this.config.fishermanMode) {
385
+ // bail out early and don't save attestations to the pool in fisherman mode
386
+ this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
387
+ ...proposalInfo,
388
+ attestors: attestors.map((a)=>a.toString())
389
+ });
390
+ return undefined;
391
+ }
392
+ return await this.createCheckpointAttestationsFromProposal(proposal, attestors);
393
+ }
394
+ /**
395
+ * Checks if we should attest to a slot based on equivocation prevention rules.
396
+ * @returns true if we should attest, false if we should skip
397
+ */ shouldAttestToSlot(slotNumber) {
398
+ // If attestToEquivocatedProposals is true, always allow
399
+ if (this.config.attestToEquivocatedProposals) {
400
+ return true;
401
+ }
402
+ // Check if incoming slot is strictly greater than last attested
403
+ if (this.lastAttestedProposal && slotNumber <= this.lastAttestedProposal.slotNumber) {
404
+ this.log.warn(`Refusing to process a proposal for slot ${slotNumber} given we already attested to a proposal for slot ${this.lastAttestedProposal.slotNumber}`);
405
+ return false;
406
+ }
407
+ return true;
408
+ }
409
+ async createCheckpointAttestationsFromProposal(proposal, attestors = []) {
410
+ // Equivocation check: must happen right before signing to minimize the race window
411
+ if (!this.shouldAttestToSlot(proposal.slotNumber)) {
412
+ return undefined;
413
+ }
414
+ const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
415
+ // Track the proposal we attested to (to prevent equivocation)
416
+ this.lastAttestedProposal = proposal;
417
+ await this.p2pClient.addOwnCheckpointAttestations(attestations);
418
+ return attestations;
419
+ }
420
+ /**
421
+ * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
422
+ * @returns Validation result with isValid flag and reason if invalid.
423
+ */ async validateCheckpointProposal(proposal, proposalInfo) {
424
+ const slot = proposal.slotNumber;
425
+ // Timeout block syncing at the start of the next slot
426
+ const config = this.checkpointsBuilder.getConfig();
427
+ const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
428
+ const timeoutSeconds = Math.max(1, nextSlotTimestampSeconds - Math.floor(this.dateProvider.now() / 1000));
429
+ // Wait for last block to sync by archive
430
+ let lastBlockHeader;
431
+ try {
432
+ lastBlockHeader = await retryUntil(async ()=>{
433
+ await this.blockSource.syncImmediate();
434
+ return this.blockSource.getBlockHeaderByArchive(proposal.archive);
435
+ }, `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`, timeoutSeconds, 0.5);
436
+ } catch (err) {
437
+ if (err instanceof TimeoutError) {
438
+ this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
439
+ return {
440
+ isValid: false,
441
+ reason: 'last_block_not_found'
442
+ };
443
+ }
444
+ this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
445
+ return {
446
+ isValid: false,
447
+ reason: 'block_fetch_error'
448
+ };
449
+ }
450
+ if (!lastBlockHeader) {
451
+ this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
452
+ return {
453
+ isValid: false,
454
+ reason: 'last_block_not_found'
455
+ };
456
+ }
457
+ // Get all full blocks for the slot and checkpoint
458
+ const blocks = await this.blockSource.getBlocksForSlot(slot);
459
+ if (blocks.length === 0) {
460
+ this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
461
+ return {
462
+ isValid: false,
463
+ reason: 'no_blocks_for_slot'
464
+ };
465
+ }
466
+ // Ensure the last block for this slot matches the archive in the checkpoint proposal
467
+ if (!blocks.at(-1)?.archive.root.equals(proposal.archive)) {
468
+ this.log.warn(`Last block archive mismatch for checkpoint proposal`, proposalInfo);
469
+ return {
470
+ isValid: false,
471
+ reason: 'last_block_archive_mismatch'
472
+ };
473
+ }
474
+ this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
475
+ ...proposalInfo,
476
+ blockNumbers: blocks.map((b)=>b.number)
477
+ });
478
+ // Get checkpoint constants from first block
479
+ const firstBlock = blocks[0];
480
+ const constants = this.extractCheckpointConstants(firstBlock);
481
+ const checkpointNumber = firstBlock.checkpointNumber;
482
+ // Get L1-to-L2 messages for this checkpoint
483
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
484
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
485
+ const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
486
+ const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch)).filter((c)=>c.checkpointNumber < checkpointNumber).map((c)=>c.checkpointOutHash);
487
+ // Fork world state at the block before the first block
488
+ const parentBlockNumber = BlockNumber(firstBlock.number - 1);
489
+ const fork = await this.worldState.fork(parentBlockNumber);
490
+ try {
491
+ // Create checkpoint builder with all existing blocks
492
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(checkpointNumber, constants, proposal.feeAssetPriceModifier, l1ToL2Messages, previousCheckpointOutHashes, fork, blocks, this.log.getBindings());
493
+ // Complete the checkpoint to get computed values
494
+ const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
495
+ // Compare checkpoint header with proposal
496
+ if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
497
+ this.log.warn(`Checkpoint header mismatch`, {
498
+ ...proposalInfo,
499
+ computed: computedCheckpoint.header.toInspect(),
500
+ proposal: proposal.checkpointHeader.toInspect()
501
+ });
502
+ return {
503
+ isValid: false,
504
+ reason: 'checkpoint_header_mismatch'
505
+ };
506
+ }
507
+ // Compare archive root with proposal
508
+ if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
509
+ this.log.warn(`Archive root mismatch`, {
510
+ ...proposalInfo,
511
+ computed: computedCheckpoint.archive.root.toString(),
512
+ proposal: proposal.archive.toString()
513
+ });
514
+ return {
515
+ isValid: false,
516
+ reason: 'archive_mismatch'
517
+ };
518
+ }
519
+ // Check that the accumulated epoch out hash matches the value in the proposal.
520
+ // The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
521
+ const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
522
+ const computedEpochOutHash = accumulateCheckpointOutHashes([
523
+ ...previousCheckpointOutHashes,
524
+ checkpointOutHash
525
+ ]);
526
+ const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
527
+ if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
528
+ this.log.warn(`Epoch out hash mismatch`, {
529
+ proposalEpochOutHash: proposalEpochOutHash.toString(),
530
+ computedEpochOutHash: computedEpochOutHash.toString(),
531
+ checkpointOutHash: checkpointOutHash.toString(),
532
+ previousCheckpointOutHashes: previousCheckpointOutHashes.map((h)=>h.toString()),
533
+ ...proposalInfo
534
+ });
535
+ return {
536
+ isValid: false,
537
+ reason: 'out_hash_mismatch'
538
+ };
539
+ }
540
+ this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
541
+ return {
542
+ isValid: true
543
+ };
544
+ } finally{
545
+ await fork.close();
546
+ }
547
+ }
548
+ /**
549
+ * Extract checkpoint global variables from a block.
550
+ */ extractCheckpointConstants(block) {
551
+ const gv = block.header.globalVariables;
552
+ return {
553
+ chainId: gv.chainId,
554
+ version: gv.version,
555
+ slotNumber: gv.slotNumber,
556
+ timestamp: gv.timestamp,
557
+ coinbase: gv.coinbase,
558
+ feeRecipient: gv.feeRecipient,
559
+ gasFees: gv.gasFees
560
+ };
561
+ }
562
+ /**
563
+ * Uploads blobs for a checkpoint to the filestore (fire and forget).
564
+ */ async uploadBlobsForCheckpoint(proposal, proposalInfo) {
565
+ try {
566
+ const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
567
+ if (!lastBlockHeader) {
568
+ this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
569
+ return;
570
+ }
571
+ const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
572
+ if (blocks.length === 0) {
573
+ this.log.warn(`No blocks found for blob upload`, proposalInfo);
574
+ return;
575
+ }
576
+ const blobFields = blocks.flatMap((b)=>b.toBlobFields());
577
+ const blobs = await getBlobsPerL1Block(blobFields);
578
+ await this.blobClient.sendBlobsToFilestore(blobs);
579
+ this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
580
+ ...proposalInfo,
581
+ numBlobs: blobs.length
582
+ });
583
+ } catch (err) {
584
+ this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
585
+ }
220
586
  }
221
587
  slashInvalidBlock(proposal) {
222
588
  const proposer = proposal.getSender();
@@ -236,39 +602,107 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
236
602
  validator: proposer,
237
603
  amount: this.config.slashBroadcastedInvalidBlockPenalty,
238
604
  offenseType: OffenseType.BROADCASTED_INVALID_BLOCK_PROPOSAL,
239
- epochOrSlot: proposal.slotNumber.toBigInt()
605
+ epochOrSlot: BigInt(proposal.slotNumber)
606
+ }
607
+ ]);
608
+ }
609
+ /**
610
+ * Handle detection of a duplicate proposal (equivocation).
611
+ * Emits a slash event when a proposer sends multiple proposals for the same position.
612
+ */ handleDuplicateProposal(info) {
613
+ const { slot, proposer, type } = info;
614
+ this.log.warn(`Triggering slash event for duplicate ${type} proposal from ${proposer.toString()} at slot ${slot}`, {
615
+ proposer: proposer.toString(),
616
+ slot,
617
+ type
618
+ });
619
+ // Emit slash event
620
+ this.emit(WANT_TO_SLASH_EVENT, [
621
+ {
622
+ validator: proposer,
623
+ amount: this.config.slashDuplicateProposalPenalty,
624
+ offenseType: OffenseType.DUPLICATE_PROPOSAL,
625
+ epochOrSlot: BigInt(slot)
240
626
  }
241
627
  ]);
242
628
  }
243
- async createBlockProposal(blockNumber, header, archive, stateReference, txs, proposerAddress, options) {
244
- if (this.previousProposal?.slotNumber.equals(header.slotNumber)) {
245
- this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
246
- return Promise.resolve(undefined);
629
+ /**
630
+ * Handle detection of a duplicate attestation (equivocation).
631
+ * Emits a slash event when an attester signs attestations for different proposals at the same slot.
632
+ */ handleDuplicateAttestation(info) {
633
+ const { slot, attester } = info;
634
+ this.log.warn(`Triggering slash event for duplicate attestation from ${attester.toString()} at slot ${slot}`, {
635
+ attester: attester.toString(),
636
+ slot
637
+ });
638
+ this.emit(WANT_TO_SLASH_EVENT, [
639
+ {
640
+ validator: attester,
641
+ amount: this.config.slashDuplicateAttestationPenalty,
642
+ offenseType: OffenseType.DUPLICATE_ATTESTATION,
643
+ epochOrSlot: BigInt(slot)
644
+ }
645
+ ]);
646
+ }
647
+ async createBlockProposal(blockHeader, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, options = {}) {
648
+ // Validate that we're not creating a proposal for an older or equal position
649
+ if (this.lastProposedBlock) {
650
+ const lastSlot = this.lastProposedBlock.slotNumber;
651
+ const lastIndex = this.lastProposedBlock.indexWithinCheckpoint;
652
+ const newSlot = blockHeader.globalVariables.slotNumber;
653
+ if (newSlot < lastSlot || newSlot === lastSlot && indexWithinCheckpoint <= lastIndex) {
654
+ throw new Error(`Cannot create block proposal for slot ${newSlot} index ${indexWithinCheckpoint}: ` + `already proposed block for slot ${lastSlot} index ${lastIndex}`);
655
+ }
247
656
  }
248
- const newProposal = await this.validationService.createBlockProposal(header, archive, stateReference, txs, proposerAddress, {
657
+ this.log.info(`Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`);
658
+ const newProposal = await this.validationService.createBlockProposal(blockHeader, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, {
249
659
  ...options,
250
660
  broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
251
661
  });
252
- this.previousProposal = newProposal;
662
+ this.lastProposedBlock = newProposal;
663
+ return newProposal;
664
+ }
665
+ async createCheckpointProposal(checkpointHeader, archive, feeAssetPriceModifier, lastBlockInfo, proposerAddress, options = {}) {
666
+ // Validate that we're not creating a proposal for an older or equal slot
667
+ if (this.lastProposedCheckpoint) {
668
+ const lastSlot = this.lastProposedCheckpoint.slotNumber;
669
+ const newSlot = checkpointHeader.slotNumber;
670
+ if (newSlot <= lastSlot) {
671
+ throw new Error(`Cannot create checkpoint proposal for slot ${newSlot}: ` + `already proposed checkpoint for slot ${lastSlot}`);
672
+ }
673
+ }
674
+ this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
675
+ const newProposal = await this.validationService.createCheckpointProposal(checkpointHeader, archive, feeAssetPriceModifier, lastBlockInfo, proposerAddress, options);
676
+ this.lastProposedCheckpoint = newProposal;
253
677
  return newProposal;
254
678
  }
255
679
  async broadcastBlockProposal(proposal) {
256
680
  await this.p2pClient.broadcastProposal(proposal);
257
681
  }
258
- async signAttestationsAndSigners(attestationsAndSigners, proposer) {
259
- return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
682
+ async signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber) {
683
+ return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber);
260
684
  }
261
685
  async collectOwnAttestations(proposal) {
262
- const slot = proposal.payload.header.slotNumber.toBigInt();
686
+ const slot = proposal.slotNumber;
263
687
  const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
264
688
  this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, {
265
689
  inCommittee
266
690
  });
267
- return this.createBlockAttestationsFromProposal(proposal, inCommittee);
691
+ const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
692
+ if (!attestations) {
693
+ return [];
694
+ }
695
+ // We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
696
+ // other nodes can see that our validators did attest to this block proposal, and do not slash us
697
+ // due to inactivity for missed attestations.
698
+ void this.p2pClient.broadcastCheckpointAttestations(attestations).catch((err)=>{
699
+ this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
700
+ });
701
+ return attestations;
268
702
  }
269
703
  async collectAttestations(proposal, required, deadline) {
270
- // Wait and poll the p2pClient's attestation pool for this block until we have enough attestations
271
- const slot = proposal.payload.header.slotNumber.toBigInt();
704
+ // Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
705
+ const slot = proposal.slotNumber;
272
706
  this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
273
707
  if (+deadline < this.dateProvider.now()) {
274
708
  this.log.error(`Deadline ${deadline.toISOString()} for collecting ${required} attestations for slot ${slot} is in the past`);
@@ -279,13 +713,13 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
279
713
  const myAddresses = this.getValidatorAddresses();
280
714
  let attestations = [];
281
715
  while(true){
282
- // Filter out attestations with a mismatching payload. This should NOT happen since we have verified
716
+ // Filter out attestations with a mismatching archive. This should NOT happen since we have verified
283
717
  // the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
284
- const collectedAttestations = (await this.p2pClient.getAttestationsForSlot(slot, proposalId)).filter((attestation)=>{
285
- if (!attestation.payload.equals(proposal.payload)) {
286
- this.log.warn(`Received attestation for slot ${slot} with mismatched payload from ${attestation.getSender()?.toString()}`, {
287
- attestationPayload: attestation.payload,
288
- proposalPayload: proposal.payload
718
+ const collectedAttestations = (await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalId)).filter((attestation)=>{
719
+ if (!attestation.archive.equals(proposal.archive)) {
720
+ this.log.warn(`Received attestation for slot ${slot} with mismatched archive from ${attestation.getSender()?.toString()}`, {
721
+ attestationArchive: attestation.archive.toString(),
722
+ proposalArchive: proposal.archive.toString()
289
723
  });
290
724
  return false;
291
725
  }
@@ -317,11 +751,6 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
317
751
  await sleep(this.config.attestationPollingIntervalMs);
318
752
  }
319
753
  }
320
- async createBlockAttestationsFromProposal(proposal, attestors = []) {
321
- const attestations = await this.validationService.attestToProposal(proposal, attestors);
322
- await this.p2pClient.addAttestations(attestations);
323
- return attestations;
324
- }
325
754
  async handleAuthRequest(peer, msg) {
326
755
  const authRequest = AuthRequest.fromBuffer(msg);
327
756
  const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch((_)=>undefined);
@@ -336,7 +765,11 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
336
765
  return Buffer.alloc(0);
337
766
  }
338
767
  const payloadToSign = authRequest.getPayloadToSign();
339
- const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign);
768
+ // AUTH_REQUEST doesn't require HA protection - multiple signatures are safe
769
+ const context = {
770
+ dutyType: DutyType.AUTH_REQUEST
771
+ };
772
+ const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign, context);
340
773
  const authResponse = new AuthResponse(statusMessage, signature);
341
774
  return authResponse.toBuffer();
342
775
  }