@aztec/validator-client 0.0.1-commit.fce3e4f → 0.0.1-commit.ff7989d6c

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 (69) hide show
  1. package/README.md +285 -0
  2. package/dest/block_proposal_handler.d.ts +24 -13
  3. package/dest/block_proposal_handler.d.ts.map +1 -1
  4. package/dest/block_proposal_handler.js +329 -87
  5. package/dest/checkpoint_builder.d.ts +66 -0
  6. package/dest/checkpoint_builder.d.ts.map +1 -0
  7. package/dest/checkpoint_builder.js +175 -0
  8. package/dest/config.d.ts +1 -1
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +16 -8
  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 +111 -28
  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 +3 -1
  18. package/dest/index.d.ts.map +1 -1
  19. package/dest/index.js +2 -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 -5
  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/tx_validator/index.d.ts +3 -0
  41. package/dest/tx_validator/index.d.ts.map +1 -0
  42. package/dest/tx_validator/index.js +2 -0
  43. package/dest/tx_validator/nullifier_cache.d.ts +14 -0
  44. package/dest/tx_validator/nullifier_cache.d.ts.map +1 -0
  45. package/dest/tx_validator/nullifier_cache.js +24 -0
  46. package/dest/tx_validator/tx_validator_factory.d.ts +19 -0
  47. package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -0
  48. package/dest/tx_validator/tx_validator_factory.js +54 -0
  49. package/dest/validator.d.ts +74 -21
  50. package/dest/validator.d.ts.map +1 -1
  51. package/dest/validator.js +452 -56
  52. package/package.json +23 -13
  53. package/src/block_proposal_handler.ts +253 -59
  54. package/src/checkpoint_builder.ts +321 -0
  55. package/src/config.ts +15 -7
  56. package/src/duties/validation_service.ts +162 -33
  57. package/src/factory.ts +17 -8
  58. package/src/index.ts +2 -0
  59. package/src/key_store/ha_key_store.ts +269 -0
  60. package/src/key_store/index.ts +1 -0
  61. package/src/key_store/interface.ts +44 -5
  62. package/src/key_store/local_key_store.ts +14 -5
  63. package/src/key_store/node_keystore_adapter.ts +28 -5
  64. package/src/key_store/web3signer_key_store.ts +18 -5
  65. package/src/metrics.ts +45 -33
  66. package/src/tx_validator/index.ts +2 -0
  67. package/src/tx_validator/nullifier_cache.ts +30 -0
  68. package/src/tx_validator/tx_validator_factory.ts +154 -0
  69. package/src/validator.ts +619 -85
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,7 +36,13 @@ 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
47
  tracer;
32
48
  validationService;
@@ -34,13 +50,14 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
34
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.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();
44
61
  // Create child logger with fisherman prefix if in fisherman mode
45
62
  this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
46
63
  this.tracer = telemetry.getTracer('Validator');
@@ -94,13 +111,29 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
94
111
  this.log.error(`Error updating epoch committee`, err);
95
112
  }
96
113
  }
97
- 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()) {
98
115
  const metrics = new ValidatorMetrics(telemetry);
99
116
  const blockProposalValidator = new BlockProposalValidator(epochCache, {
100
117
  txsPermitted: !config.disableTransactions
101
118
  });
102
- const blockProposalHandler = new BlockProposalHandler(blockBuilder, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, config, metrics, dateProvider, telemetry);
103
- 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);
104
137
  return validator;
105
138
  }
106
139
  getValidatorAddresses() {
@@ -109,12 +142,8 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
109
142
  getBlockProposalHandler() {
110
143
  return this.blockProposalHandler;
111
144
  }
112
- // Proxy method for backwards compatibility with tests
113
- reExecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages) {
114
- return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
115
- }
116
- signWithAddress(addr, msg) {
117
- return this.keyStore.signTypedDataWithAddress(addr, msg);
145
+ signWithAddress(addr, msg, context) {
146
+ return this.keyStore.signTypedDataWithAddress(addr, msg, context);
118
147
  }
119
148
  getCoinbaseForAttestor(attestor) {
120
149
  return this.keyStore.getCoinbaseAddress(attestor);
@@ -131,11 +160,26 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
131
160
  ...config
132
161
  };
133
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
+ }
134
177
  async start() {
135
178
  if (this.epochCacheUpdateLoop.isRunning()) {
136
179
  this.log.warn(`Validator client already started`);
137
180
  return;
138
181
  }
182
+ await this.keyStore.start();
139
183
  await this.registerHandlers();
140
184
  const myAddresses = this.getValidatorAddresses();
141
185
  const inCommittee = await this.epochCache.filterInCommittee('now', myAddresses);
@@ -148,46 +192,75 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
148
192
  }
149
193
  async stop() {
150
194
  await this.epochCacheUpdateLoop.stop();
195
+ await this.keyStore.stop();
151
196
  }
152
197
  /** Register handlers on the p2p client */ async registerHandlers() {
153
198
  if (!this.hasRegisteredHandlers) {
154
199
  this.hasRegisteredHandlers = true;
155
200
  this.log.debug(`Registering validator handlers for p2p client`);
156
- const handler = (block, proposalSender)=>this.attestToProposal(block, proposalSender);
157
- 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
+ });
158
217
  const myAddresses = this.getValidatorAddresses();
159
218
  this.p2pClient.registerThisValidatorAddresses(myAddresses);
160
219
  await this.p2pClient.addReqRespSubProtocol(ReqRespSubProtocol.AUTH, this.handleAuthRequest.bind(this));
161
220
  }
162
221
  }
163
- async attestToProposal(proposal, proposalSender) {
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) {
164
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);
165
231
  const proposer = proposal.getSender();
166
232
  // Reject proposals with invalid signatures
167
233
  if (!proposer) {
168
- this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
169
- return undefined;
234
+ this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
235
+ return false;
170
236
  }
171
- // 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)
172
246
  const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
173
247
  const partOfCommittee = inCommittee.length > 0;
174
248
  const proposalInfo = {
175
249
  ...proposal.toBlockInfo(),
176
250
  proposer: proposer.toString()
177
251
  };
178
- this.log.info(`Received proposal for slot ${slotNumber}`, {
252
+ this.log.info(`Received block proposal for slot ${slotNumber}`, {
179
253
  ...proposalInfo,
180
254
  txHashes: proposal.txHashes.map((t)=>t.toString()),
181
255
  fishermanMode: this.config.fishermanMode || false
182
256
  });
183
- // Reexecute txs if we are part of the committee so we can attest, or if slashing is enabled so we can slash
184
- // invalid proposals even when not in the committee, or if we are configured to always reexecute for monitoring purposes.
257
+ // Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
185
258
  // In fisherman mode, we always reexecute to validate proposals.
186
259
  const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } = this.config;
187
- const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals;
188
- const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute);
260
+ const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals || this.blobClient.canUpload();
261
+ const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute && !escapeHatchOpen);
189
262
  if (!validationResult.isValid) {
190
- this.log.warn(`Proposal validation failed: ${validationResult.reason}`, proposalInfo);
263
+ this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
191
264
  const reason = validationResult.reason || 'unknown';
192
265
  // Classify failure reason: bad proposal vs node issue
193
266
  const badProposalReasons = [
@@ -200,30 +273,100 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
200
273
  if (badProposalReasons.includes(reason)) {
201
274
  this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
202
275
  } else {
203
- // Node issues so we can't attest
276
+ // Node issues so we can't validate
204
277
  this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
205
278
  }
206
279
  // Slash invalid block proposals (can happen even when not in committee)
207
- 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) {
208
281
  this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
209
282
  this.slashInvalidBlock(proposal);
210
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}`);
211
327
  return undefined;
212
328
  }
213
329
  // Check that I have any address in current committee before attesting
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
214
358
  // In fisherman mode, we still create attestations for validation even if not in committee
215
359
  if (!partOfCommittee && !this.config.fishermanMode) {
216
360
  this.log.verbose(`No validator in the current committee, skipping attestation`, proposalInfo);
217
361
  return undefined;
218
362
  }
219
363
  // Provided all of the above checks pass, we can attest to the proposal
220
- this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} proposal for slot ${slotNumber}`, {
364
+ this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
221
365
  ...proposalInfo,
222
366
  inCommittee: partOfCommittee,
223
367
  fishermanMode: this.config.fishermanMode || false
224
368
  });
225
369
  this.metrics.incSuccessfulAttestations(inCommittee.length);
226
- // If the above function does not throw an error, then we can attest to the proposal
227
370
  // Determine which validators should attest
228
371
  let attestors;
229
372
  if (partOfCommittee) {
@@ -240,13 +383,206 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
240
383
  }
241
384
  if (this.config.fishermanMode) {
242
385
  // bail out early and don't save attestations to the pool in fisherman mode
243
- this.log.info(`Creating attestations for proposal for slot ${slotNumber}`, {
386
+ this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
244
387
  ...proposalInfo,
245
388
  attestors: attestors.map((a)=>a.toString())
246
389
  });
247
390
  return undefined;
248
391
  }
249
- return this.createBlockAttestationsFromProposal(proposal, attestors);
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
+ }
250
586
  }
251
587
  slashInvalidBlock(proposal) {
252
588
  const proposer = proposal.getSender();
@@ -270,42 +606,103 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
270
606
  }
271
607
  ]);
272
608
  }
273
- async createBlockProposal(blockNumber, header, archive, txs, proposerAddress, options) {
274
- if (this.previousProposal?.slotNumber === header.slotNumber) {
275
- this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
276
- return Promise.resolve(undefined);
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)
626
+ }
627
+ ]);
628
+ }
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
+ }
277
656
  }
278
- const newProposal = await this.validationService.createBlockProposal(header, archive, 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, {
279
659
  ...options,
280
660
  broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
281
661
  });
282
- 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;
283
677
  return newProposal;
284
678
  }
285
679
  async broadcastBlockProposal(proposal) {
286
680
  await this.p2pClient.broadcastProposal(proposal);
287
681
  }
288
- async signAttestationsAndSigners(attestationsAndSigners, proposer) {
289
- return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
682
+ async signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber) {
683
+ return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber);
290
684
  }
291
685
  async collectOwnAttestations(proposal) {
292
- const slot = proposal.payload.header.slotNumber;
686
+ const slot = proposal.slotNumber;
293
687
  const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
294
688
  this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, {
295
689
  inCommittee
296
690
  });
297
- const attestations = await this.createBlockAttestationsFromProposal(proposal, inCommittee);
691
+ const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
692
+ if (!attestations) {
693
+ return [];
694
+ }
298
695
  // We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
299
696
  // other nodes can see that our validators did attest to this block proposal, and do not slash us
300
697
  // due to inactivity for missed attestations.
301
- void this.p2pClient.broadcastAttestations(attestations).catch((err)=>{
698
+ void this.p2pClient.broadcastCheckpointAttestations(attestations).catch((err)=>{
302
699
  this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
303
700
  });
304
701
  return attestations;
305
702
  }
306
703
  async collectAttestations(proposal, required, deadline) {
307
- // Wait and poll the p2pClient's attestation pool for this block until we have enough attestations
308
- const slot = proposal.payload.header.slotNumber;
704
+ // Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
705
+ const slot = proposal.slotNumber;
309
706
  this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
310
707
  if (+deadline < this.dateProvider.now()) {
311
708
  this.log.error(`Deadline ${deadline.toISOString()} for collecting ${required} attestations for slot ${slot} is in the past`);
@@ -316,13 +713,13 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
316
713
  const myAddresses = this.getValidatorAddresses();
317
714
  let attestations = [];
318
715
  while(true){
319
- // 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
320
717
  // the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
321
- const collectedAttestations = (await this.p2pClient.getAttestationsForSlot(slot, proposalId)).filter((attestation)=>{
322
- if (!attestation.payload.equals(proposal.payload)) {
323
- this.log.warn(`Received attestation for slot ${slot} with mismatched payload from ${attestation.getSender()?.toString()}`, {
324
- attestationPayload: attestation.payload,
325
- 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()
326
723
  });
327
724
  return false;
328
725
  }
@@ -354,11 +751,6 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
354
751
  await sleep(this.config.attestationPollingIntervalMs);
355
752
  }
356
753
  }
357
- async createBlockAttestationsFromProposal(proposal, attestors = []) {
358
- const attestations = await this.validationService.attestToProposal(proposal, attestors);
359
- await this.p2pClient.addAttestations(attestations);
360
- return attestations;
361
- }
362
754
  async handleAuthRequest(peer, msg) {
363
755
  const authRequest = AuthRequest.fromBuffer(msg);
364
756
  const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch((_)=>undefined);
@@ -373,7 +765,11 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
373
765
  return Buffer.alloc(0);
374
766
  }
375
767
  const payloadToSign = authRequest.getPayloadToSign();
376
- 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);
377
773
  const authResponse = new AuthResponse(statusMessage, signature);
378
774
  return authResponse.toBuffer();
379
775
  }