@aztec/validator-client 0.0.1-commit.c7c42ec → 0.0.1-commit.cf93bcc56

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