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

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 +282 -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 +349 -89
  5. package/dest/checkpoint_builder.d.ts +70 -0
  6. package/dest/checkpoint_builder.d.ts.map +1 -0
  7. package/dest/checkpoint_builder.js +167 -0
  8. package/dest/config.d.ts +1 -1
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +16 -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 +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 -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/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 +48 -20
  50. package/dest/validator.d.ts.map +1 -1
  51. package/dest/validator.js +359 -63
  52. package/package.json +24 -14
  53. package/src/block_proposal_handler.ts +278 -66
  54. package/src/checkpoint_builder.ts +310 -0
  55. package/src/config.ts +16 -6
  56. package/src/duties/validation_service.ts +157 -38
  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 +483 -95
package/src/validator.ts CHANGED
@@ -1,30 +1,60 @@
1
+ import type { BlobClientInterface } from '@aztec/blob-client/client';
2
+ import { type Blob, getBlobsPerL1Block } from '@aztec/blob-lib';
1
3
  import type { EpochCache } from '@aztec/epoch-cache';
4
+ import {
5
+ BlockNumber,
6
+ CheckpointNumber,
7
+ EpochNumber,
8
+ IndexWithinCheckpoint,
9
+ SlotNumber,
10
+ } from '@aztec/foundation/branded-types';
11
+ import { Fr } from '@aztec/foundation/curves/bn254';
12
+ import { TimeoutError } from '@aztec/foundation/error';
2
13
  import type { EthAddress } from '@aztec/foundation/eth-address';
3
14
  import type { Signature } from '@aztec/foundation/eth-signature';
4
- import { Fr } from '@aztec/foundation/fields';
5
- import { type Logger, createLogger } from '@aztec/foundation/log';
15
+ import { type LogData, type Logger, createLogger } from '@aztec/foundation/log';
16
+ import { retryUntil } from '@aztec/foundation/retry';
6
17
  import { RunningPromise } from '@aztec/foundation/running-promise';
7
18
  import { sleep } from '@aztec/foundation/sleep';
8
19
  import { DateProvider } from '@aztec/foundation/timer';
9
20
  import type { KeystoreManager } from '@aztec/node-keystore';
10
- import type { P2P, PeerId, TxProvider } from '@aztec/p2p';
21
+ import type { P2P, PeerId } from '@aztec/p2p';
11
22
  import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
12
23
  import { OffenseType, WANT_TO_SLASH_EVENT, type Watcher, type WatcherEmitter } from '@aztec/slasher';
13
24
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
14
- import type { CommitteeAttestationsAndSigners, L2BlockSource } from '@aztec/stdlib/block';
15
- import type { IFullNodeBlockBuilder, Validator, ValidatorClientFullConfig } from '@aztec/stdlib/interfaces/server';
16
- import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
17
- import type { BlockAttestation, BlockProposal, BlockProposalOptions } from '@aztec/stdlib/p2p';
25
+ import type { CommitteeAttestationsAndSigners, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
26
+ import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
27
+ import type {
28
+ CreateCheckpointProposalLastBlockData,
29
+ ITxProvider,
30
+ Validator,
31
+ ValidatorClientFullConfig,
32
+ WorldStateSynchronizer,
33
+ } from '@aztec/stdlib/interfaces/server';
34
+ import { type L1ToL2MessageSource, accumulateCheckpointOutHashes } from '@aztec/stdlib/messaging';
35
+ import type {
36
+ BlockProposal,
37
+ BlockProposalOptions,
38
+ CheckpointAttestation,
39
+ CheckpointProposalCore,
40
+ CheckpointProposalOptions,
41
+ } from '@aztec/stdlib/p2p';
42
+ import { CheckpointProposal } from '@aztec/stdlib/p2p';
18
43
  import type { CheckpointHeader } from '@aztec/stdlib/rollup';
19
- import type { StateReference, Tx } from '@aztec/stdlib/tx';
44
+ import type { BlockHeader, CheckpointGlobalVariables, Tx } from '@aztec/stdlib/tx';
20
45
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
21
46
  import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
47
+ import { createHASigner } from '@aztec/validator-ha-signer/factory';
48
+ import { DutyType, type SigningContext } from '@aztec/validator-ha-signer/types';
22
49
 
23
50
  import { EventEmitter } from 'events';
24
51
  import type { TypedDataDefinition } from 'viem';
25
52
 
26
53
  import { BlockProposalHandler, type BlockProposalValidationFailureReason } from './block_proposal_handler.js';
54
+ import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
27
55
  import { ValidationService } from './duties/validation_service.js';
56
+ import { HAKeyStore } from './key_store/ha_key_store.js';
57
+ import type { ExtendedValidatorKeyStore } from './key_store/interface.js';
28
58
  import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
29
59
  import { ValidatorMetrics } from './metrics.js';
30
60
 
@@ -45,6 +75,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
45
75
  public readonly tracer: Tracer;
46
76
  private validationService: ValidationService;
47
77
  private metrics: ValidatorMetrics;
78
+ private log: Logger;
48
79
 
49
80
  // Whether it has already registered handlers on the p2p client
50
81
  private hasRegisteredHandlers = false;
@@ -52,29 +83,38 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
52
83
  // Used to check if we are sending the same proposal twice
53
84
  private previousProposal?: BlockProposal;
54
85
 
55
- private lastEpochForCommitteeUpdateLoop: bigint | undefined;
86
+ private lastEpochForCommitteeUpdateLoop: EpochNumber | undefined;
56
87
  private epochCacheUpdateLoop: RunningPromise;
57
88
 
58
89
  private proposersOfInvalidBlocks: Set<string> = new Set();
59
90
 
60
91
  protected constructor(
61
- private keyStore: NodeKeystoreAdapter,
92
+ private keyStore: ExtendedValidatorKeyStore,
62
93
  private epochCache: EpochCache,
63
94
  private p2pClient: P2P,
64
95
  private blockProposalHandler: BlockProposalHandler,
96
+ private blockSource: L2BlockSource,
97
+ private checkpointsBuilder: FullNodeCheckpointsBuilder,
98
+ private worldState: WorldStateSynchronizer,
99
+ private l1ToL2MessageSource: L1ToL2MessageSource,
65
100
  private config: ValidatorClientFullConfig,
101
+ private blobClient: BlobClientInterface,
66
102
  private dateProvider: DateProvider = new DateProvider(),
67
103
  telemetry: TelemetryClient = getTelemetryClient(),
68
- private log = createLogger('validator'),
104
+ log = createLogger('validator'),
69
105
  ) {
70
106
  super();
107
+
108
+ // Create child logger with fisherman prefix if in fisherman mode
109
+ this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
110
+
71
111
  this.tracer = telemetry.getTracer('Validator');
72
112
  this.metrics = new ValidatorMetrics(telemetry);
73
113
 
74
- this.validationService = new ValidationService(keyStore, log.createChild('validation-service'));
114
+ this.validationService = new ValidationService(keyStore, this.log.createChild('validation-service'));
75
115
 
76
116
  // Refresh epoch cache every second to trigger alert if participation in committee changes
77
- this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), log, 1000);
117
+ this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), this.log, 1000);
78
118
 
79
119
  const myAddresses = this.getValidatorAddresses();
80
120
  this.log.verbose(`Initialized validator with addresses: ${myAddresses.map(a => a.toString()).join(', ')}`);
@@ -132,15 +172,17 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
132
172
  }
133
173
  }
134
174
 
135
- static new(
175
+ static async new(
136
176
  config: ValidatorClientFullConfig,
137
- blockBuilder: IFullNodeBlockBuilder,
177
+ checkpointsBuilder: FullNodeCheckpointsBuilder,
178
+ worldState: WorldStateSynchronizer,
138
179
  epochCache: EpochCache,
139
180
  p2pClient: P2P,
140
- blockSource: L2BlockSource,
181
+ blockSource: L2BlockSource & L2BlockSink,
141
182
  l1ToL2MessageSource: L1ToL2MessageSource,
142
- txProvider: TxProvider,
183
+ txProvider: ITxProvider,
143
184
  keyStoreManager: KeystoreManager,
185
+ blobClient: BlobClientInterface,
144
186
  dateProvider: DateProvider = new DateProvider(),
145
187
  telemetry: TelemetryClient = getTelemetryClient(),
146
188
  ) {
@@ -149,23 +191,41 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
149
191
  txsPermitted: !config.disableTransactions,
150
192
  });
151
193
  const blockProposalHandler = new BlockProposalHandler(
152
- blockBuilder,
194
+ checkpointsBuilder,
195
+ worldState,
153
196
  blockSource,
154
197
  l1ToL2MessageSource,
155
198
  txProvider,
156
199
  blockProposalValidator,
200
+ epochCache,
157
201
  config,
158
202
  metrics,
159
203
  dateProvider,
160
204
  telemetry,
161
205
  );
162
206
 
207
+ let validatorKeyStore: ExtendedValidatorKeyStore = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
208
+ if (config.haSigningEnabled) {
209
+ // If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
210
+ const haConfig = {
211
+ ...config,
212
+ maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000,
213
+ };
214
+ const { signer } = await createHASigner(haConfig);
215
+ validatorKeyStore = new HAKeyStore(validatorKeyStore, signer);
216
+ }
217
+
163
218
  const validator = new ValidatorClient(
164
- NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager),
219
+ validatorKeyStore,
165
220
  epochCache,
166
221
  p2pClient,
167
222
  blockProposalHandler,
223
+ blockSource,
224
+ checkpointsBuilder,
225
+ worldState,
226
+ l1ToL2MessageSource,
168
227
  config,
228
+ blobClient,
169
229
  dateProvider,
170
230
  telemetry,
171
231
  );
@@ -183,18 +243,8 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
183
243
  return this.blockProposalHandler;
184
244
  }
185
245
 
186
- // Proxy method for backwards compatibility with tests
187
- public reExecuteTransactions(
188
- proposal: BlockProposal,
189
- blockNumber: number,
190
- txs: any[],
191
- l1ToL2Messages: Fr[],
192
- ): Promise<any> {
193
- return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
194
- }
195
-
196
- public signWithAddress(addr: EthAddress, msg: TypedDataDefinition) {
197
- return this.keyStore.signTypedDataWithAddress(addr, msg);
246
+ public signWithAddress(addr: EthAddress, msg: TypedDataDefinition, context: SigningContext) {
247
+ return this.keyStore.signTypedDataWithAddress(addr, msg, context);
198
248
  }
199
249
 
200
250
  public getCoinbaseForAttestor(attestor: EthAddress): EthAddress {
@@ -219,18 +269,15 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
219
269
  return;
220
270
  }
221
271
 
272
+ await this.keyStore.start();
273
+
222
274
  await this.registerHandlers();
223
275
 
224
276
  const myAddresses = this.getValidatorAddresses();
225
277
  const inCommittee = await this.epochCache.filterInCommittee('now', myAddresses);
278
+ this.log.info(`Started validator with addresses: ${myAddresses.map(a => a.toString()).join(', ')}`);
226
279
  if (inCommittee.length > 0) {
227
- this.log.info(
228
- `Started validator with addresses in current validator committee: ${inCommittee
229
- .map(a => a.toString())
230
- .join(', ')}`,
231
- );
232
- } else {
233
- this.log.info(`Started validator with addresses: ${myAddresses.map(a => a.toString()).join(', ')}`);
280
+ this.log.info(`Addresses in current validator committee: ${inCommittee.map(a => a.toString()).join(', ')}`);
234
281
  }
235
282
  this.epochCacheUpdateLoop.start();
236
283
 
@@ -239,6 +286,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
239
286
 
240
287
  public async stop() {
241
288
  await this.epochCacheUpdateLoop.stop();
289
+ await this.keyStore.stop();
242
290
  }
243
291
 
244
292
  /** Register handlers on the p2p client */
@@ -247,9 +295,19 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
247
295
  this.hasRegisteredHandlers = true;
248
296
  this.log.debug(`Registering validator handlers for p2p client`);
249
297
 
250
- const handler = (block: BlockProposal, proposalSender: PeerId): Promise<BlockAttestation[] | undefined> =>
251
- this.attestToProposal(block, proposalSender);
252
- this.p2pClient.registerBlockProposalHandler(handler);
298
+ // Block proposal handler - validates but does NOT attest (validators only attest to checkpoints)
299
+ const blockHandler = (block: BlockProposal, proposalSender: PeerId): Promise<boolean> =>
300
+ this.validateBlockProposal(block, proposalSender);
301
+ this.p2pClient.registerBlockProposalHandler(blockHandler);
302
+
303
+ // Checkpoint proposal handler - validates and creates attestations
304
+ // The checkpoint is received as CheckpointProposalCore since the lastBlock is extracted
305
+ // and processed separately via the block handler above.
306
+ const checkpointHandler = (
307
+ checkpoint: CheckpointProposalCore,
308
+ proposalSender: PeerId,
309
+ ): Promise<CheckpointAttestation[] | undefined> => this.attestToCheckpointProposal(checkpoint, proposalSender);
310
+ this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
253
311
 
254
312
  const myAddresses = this.getValidatorAddresses();
255
313
  this.p2pClient.registerThisValidatorAddresses(myAddresses);
@@ -258,42 +316,56 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
258
316
  }
259
317
  }
260
318
 
261
- async attestToProposal(proposal: BlockProposal, proposalSender: PeerId): Promise<BlockAttestation[] | undefined> {
262
- const slotNumber = proposal.slotNumber.toBigInt();
319
+ /**
320
+ * Validate a block proposal from a peer.
321
+ * Note: Validators do NOT attest to individual blocks - attestations are only for checkpoint proposals.
322
+ * @returns true if the proposal is valid, false otherwise
323
+ */
324
+ async validateBlockProposal(proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> {
325
+ const slotNumber = proposal.slotNumber;
326
+
327
+ // Note: During escape hatch, we still want to "validate" proposals for observability,
328
+ // but we intentionally reject them and disable slashing invalid block and attestation flow.
329
+ const escapeHatchOpen = await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber);
330
+
263
331
  const proposer = proposal.getSender();
264
332
 
265
333
  // Reject proposals with invalid signatures
266
334
  if (!proposer) {
267
- this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
268
- return undefined;
335
+ this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
336
+ return false;
269
337
  }
270
338
 
271
- // Check that I have any address in current committee before attesting
339
+ // Check if we're in the committee (for metrics purposes)
272
340
  const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
273
341
  const partOfCommittee = inCommittee.length > 0;
274
342
 
275
343
  const proposalInfo = { ...proposal.toBlockInfo(), proposer: proposer.toString() };
276
- this.log.info(`Received proposal for slot ${slotNumber}`, {
344
+ this.log.info(`Received block proposal for slot ${slotNumber}`, {
277
345
  ...proposalInfo,
278
346
  txHashes: proposal.txHashes.map(t => t.toString()),
347
+ fishermanMode: this.config.fishermanMode || false,
279
348
  });
280
349
 
281
- // Reexecute txs if we are part of the committee so we can attest, or if slashing is enabled so we can slash
282
- // invalid proposals even when not in the committee, or if we are configured to always reexecute for monitoring purposes.
283
- const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals } = this.config;
350
+ // Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
351
+ // In fisherman mode, we always reexecute to validate proposals.
352
+ const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } =
353
+ this.config;
284
354
  const shouldReexecute =
355
+ fishermanMode ||
285
356
  (slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute) ||
286
357
  (partOfCommittee && validatorReexecute) ||
287
- alwaysReexecuteBlockProposals;
358
+ alwaysReexecuteBlockProposals ||
359
+ this.blobClient.canUpload();
288
360
 
289
361
  const validationResult = await this.blockProposalHandler.handleBlockProposal(
290
362
  proposal,
291
363
  proposalSender,
292
- !!shouldReexecute,
364
+ !!shouldReexecute && !escapeHatchOpen,
293
365
  );
294
366
 
295
367
  if (!validationResult.isValid) {
296
- this.log.warn(`Proposal validation failed: ${validationResult.reason}`, proposalInfo);
368
+ this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
297
369
 
298
370
  const reason = validationResult.reason || 'unknown';
299
371
  // Classify failure reason: bad proposal vs node issue
@@ -308,12 +380,13 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
308
380
  if (badProposalReasons.includes(reason as BlockProposalValidationFailureReason)) {
309
381
  this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
310
382
  } else {
311
- // Node issues so we can't attest
383
+ // Node issues so we can't validate
312
384
  this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
313
385
  }
314
386
 
315
387
  // Slash invalid block proposals (can happen even when not in committee)
316
388
  if (
389
+ !escapeHatchOpen &&
317
390
  validationResult.reason &&
318
391
  SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) &&
319
392
  slashBroadcastedInvalidBlockPenalty > 0n
@@ -321,21 +394,304 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
321
394
  this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
322
395
  this.slashInvalidBlock(proposal);
323
396
  }
397
+ return false;
398
+ }
399
+
400
+ this.log.info(`Validated block proposal for slot ${slotNumber}`, {
401
+ ...proposalInfo,
402
+ inCommittee: partOfCommittee,
403
+ fishermanMode: this.config.fishermanMode || false,
404
+ escapeHatchOpen,
405
+ });
406
+
407
+ if (escapeHatchOpen) {
408
+ this.log.warn(`Escape hatch open for slot ${slotNumber}, rejecting block proposal`, proposalInfo);
409
+ return false;
410
+ }
411
+
412
+ return true;
413
+ }
414
+
415
+ /**
416
+ * Validate and attest to a checkpoint proposal from a peer.
417
+ * The proposal is received as CheckpointProposalCore (without lastBlock) since
418
+ * the lastBlock is extracted and processed separately via the block handler.
419
+ * @returns Checkpoint attestations if valid, undefined otherwise
420
+ */
421
+ async attestToCheckpointProposal(
422
+ proposal: CheckpointProposalCore,
423
+ _proposalSender: PeerId,
424
+ ): Promise<CheckpointAttestation[] | undefined> {
425
+ const slotNumber = proposal.slotNumber;
426
+ const proposer = proposal.getSender();
427
+
428
+ // If escape hatch is open for this slot's epoch, do not attest.
429
+ if (await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber)) {
430
+ this.log.warn(`Escape hatch open for slot ${slotNumber}, skipping checkpoint attestation handling`);
324
431
  return undefined;
325
432
  }
326
433
 
434
+ // Reject proposals with invalid signatures
435
+ if (!proposer) {
436
+ this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
437
+ return undefined;
438
+ }
439
+
440
+ // Check that I have any address in current committee before attesting
441
+ const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
442
+ const partOfCommittee = inCommittee.length > 0;
443
+
444
+ const proposalInfo = {
445
+ slotNumber,
446
+ archive: proposal.archive.toString(),
447
+ proposer: proposer.toString(),
448
+ txCount: proposal.txHashes.length,
449
+ };
450
+ this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
451
+ ...proposalInfo,
452
+ txHashes: proposal.txHashes.map(t => t.toString()),
453
+ fishermanMode: this.config.fishermanMode || false,
454
+ });
455
+
456
+ // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
457
+ if (this.config.skipCheckpointProposalValidation) {
458
+ this.log.warn(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
459
+ } else {
460
+ const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
461
+ if (!validationResult.isValid) {
462
+ this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
463
+ return undefined;
464
+ }
465
+ }
466
+
467
+ // Upload blobs to filestore if we can (fire and forget)
468
+ if (this.blobClient.canUpload()) {
469
+ void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
470
+ }
471
+
327
472
  // Check that I have any address in current committee before attesting
328
- if (!partOfCommittee) {
473
+ // In fisherman mode, we still create attestations for validation even if not in committee
474
+ if (!partOfCommittee && !this.config.fishermanMode) {
329
475
  this.log.verbose(`No validator in the current committee, skipping attestation`, proposalInfo);
330
476
  return undefined;
331
477
  }
332
478
 
333
479
  // Provided all of the above checks pass, we can attest to the proposal
334
- this.log.info(`Attesting to proposal for slot ${slotNumber}`, proposalInfo);
480
+ this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
481
+ ...proposalInfo,
482
+ inCommittee: partOfCommittee,
483
+ fishermanMode: this.config.fishermanMode || false,
484
+ });
485
+
335
486
  this.metrics.incSuccessfulAttestations(inCommittee.length);
336
487
 
337
- // If the above function does not throw an error, then we can attest to the proposal
338
- return this.createBlockAttestationsFromProposal(proposal, inCommittee);
488
+ // Determine which validators should attest
489
+ let attestors: EthAddress[];
490
+ if (partOfCommittee) {
491
+ attestors = inCommittee;
492
+ } else if (this.config.fishermanMode) {
493
+ // In fisherman mode, create attestations for validation purposes even if not in committee. These won't be broadcast.
494
+ attestors = this.getValidatorAddresses();
495
+ } else {
496
+ attestors = [];
497
+ }
498
+
499
+ // Only create attestations if we have attestors
500
+ if (attestors.length === 0) {
501
+ return undefined;
502
+ }
503
+
504
+ if (this.config.fishermanMode) {
505
+ // bail out early and don't save attestations to the pool in fisherman mode
506
+ this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
507
+ ...proposalInfo,
508
+ attestors: attestors.map(a => a.toString()),
509
+ });
510
+ return undefined;
511
+ }
512
+
513
+ return this.createCheckpointAttestationsFromProposal(proposal, attestors);
514
+ }
515
+
516
+ private async createCheckpointAttestationsFromProposal(
517
+ proposal: CheckpointProposalCore,
518
+ attestors: EthAddress[] = [],
519
+ ): Promise<CheckpointAttestation[]> {
520
+ const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
521
+ await this.p2pClient.addCheckpointAttestations(attestations);
522
+ return attestations;
523
+ }
524
+
525
+ /**
526
+ * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
527
+ * @returns Validation result with isValid flag and reason if invalid.
528
+ */
529
+ private async validateCheckpointProposal(
530
+ proposal: CheckpointProposalCore,
531
+ proposalInfo: LogData,
532
+ ): Promise<{ isValid: true } | { isValid: false; reason: string }> {
533
+ const slot = proposal.slotNumber;
534
+ const timeoutSeconds = 10; // TODO(palla/mbps): This should map to the timetable settings
535
+
536
+ // Wait for last block to sync by archive
537
+ let lastBlockHeader: BlockHeader | undefined;
538
+ try {
539
+ lastBlockHeader = await retryUntil(
540
+ async () => {
541
+ await this.blockSource.syncImmediate();
542
+ return this.blockSource.getBlockHeaderByArchive(proposal.archive);
543
+ },
544
+ `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`,
545
+ timeoutSeconds,
546
+ 0.5,
547
+ );
548
+ } catch (err) {
549
+ if (err instanceof TimeoutError) {
550
+ this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
551
+ return { isValid: false, reason: 'last_block_not_found' };
552
+ }
553
+ this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
554
+ return { isValid: false, reason: 'block_fetch_error' };
555
+ }
556
+
557
+ if (!lastBlockHeader) {
558
+ this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
559
+ return { isValid: false, reason: 'last_block_not_found' };
560
+ }
561
+
562
+ // Get all full blocks for the slot and checkpoint
563
+ const blocks = await this.blockSource.getBlocksForSlot(slot);
564
+ if (blocks.length === 0) {
565
+ this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
566
+ return { isValid: false, reason: 'no_blocks_for_slot' };
567
+ }
568
+
569
+ this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
570
+ ...proposalInfo,
571
+ blockNumbers: blocks.map(b => b.number),
572
+ });
573
+
574
+ // Get checkpoint constants from first block
575
+ const firstBlock = blocks[0];
576
+ const constants = this.extractCheckpointConstants(firstBlock);
577
+ const checkpointNumber = firstBlock.checkpointNumber;
578
+
579
+ // Get L1-to-L2 messages for this checkpoint
580
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
581
+
582
+ // Compute the previous checkpoint out hashes for the epoch.
583
+ // TODO: There can be a more efficient way to get the previous checkpoint out hashes without having to fetch the
584
+ // actual checkpoints and the blocks/txs in them.
585
+ const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
586
+ const previousCheckpoints = (await this.blockSource.getCheckpointsForEpoch(epoch))
587
+ .filter(b => b.number < checkpointNumber)
588
+ .sort((a, b) => a.number - b.number);
589
+ const previousCheckpointOutHashes = previousCheckpoints.map(c => c.getCheckpointOutHash());
590
+
591
+ // Fork world state at the block before the first block
592
+ const parentBlockNumber = BlockNumber(firstBlock.number - 1);
593
+ const fork = await this.worldState.fork(parentBlockNumber);
594
+
595
+ try {
596
+ // Create checkpoint builder with all existing blocks
597
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
598
+ checkpointNumber,
599
+ constants,
600
+ l1ToL2Messages,
601
+ previousCheckpointOutHashes,
602
+ fork,
603
+ blocks,
604
+ this.log.getBindings(),
605
+ );
606
+
607
+ // Complete the checkpoint to get computed values
608
+ const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
609
+
610
+ // Compare checkpoint header with proposal
611
+ if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
612
+ this.log.warn(`Checkpoint header mismatch`, {
613
+ ...proposalInfo,
614
+ computed: computedCheckpoint.header.toInspect(),
615
+ proposal: proposal.checkpointHeader.toInspect(),
616
+ });
617
+ return { isValid: false, reason: 'checkpoint_header_mismatch' };
618
+ }
619
+
620
+ // Compare archive root with proposal
621
+ if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
622
+ this.log.warn(`Archive root mismatch`, {
623
+ ...proposalInfo,
624
+ computed: computedCheckpoint.archive.root.toString(),
625
+ proposal: proposal.archive.toString(),
626
+ });
627
+ return { isValid: false, reason: 'archive_mismatch' };
628
+ }
629
+
630
+ // Check that the accumulated epoch out hash matches the value in the proposal.
631
+ // The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
632
+ const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
633
+ const computedEpochOutHash = accumulateCheckpointOutHashes([...previousCheckpointOutHashes, checkpointOutHash]);
634
+ const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
635
+ if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
636
+ this.log.warn(`Epoch out hash mismatch`, {
637
+ proposalEpochOutHash: proposalEpochOutHash.toString(),
638
+ computedEpochOutHash: computedEpochOutHash.toString(),
639
+ checkpointOutHash: checkpointOutHash.toString(),
640
+ previousCheckpointOutHashes: previousCheckpointOutHashes.map(h => h.toString()),
641
+ ...proposalInfo,
642
+ });
643
+ return { isValid: false, reason: 'out_hash_mismatch' };
644
+ }
645
+
646
+ this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
647
+ return { isValid: true };
648
+ } finally {
649
+ await fork.close();
650
+ }
651
+ }
652
+
653
+ /**
654
+ * Extract checkpoint global variables from a block.
655
+ */
656
+ private extractCheckpointConstants(block: L2Block): CheckpointGlobalVariables {
657
+ const gv = block.header.globalVariables;
658
+ return {
659
+ chainId: gv.chainId,
660
+ version: gv.version,
661
+ slotNumber: gv.slotNumber,
662
+ coinbase: gv.coinbase,
663
+ feeRecipient: gv.feeRecipient,
664
+ gasFees: gv.gasFees,
665
+ };
666
+ }
667
+
668
+ /**
669
+ * Uploads blobs for a checkpoint to the filestore (fire and forget).
670
+ */
671
+ private async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
672
+ try {
673
+ const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
674
+ if (!lastBlockHeader) {
675
+ this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
676
+ return;
677
+ }
678
+
679
+ const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
680
+ if (blocks.length === 0) {
681
+ this.log.warn(`No blocks found for blob upload`, proposalInfo);
682
+ return;
683
+ }
684
+
685
+ const blobFields = blocks.flatMap(b => b.toBlobFields());
686
+ const blobs: Blob[] = getBlobsPerL1Block(blobFields);
687
+ await this.blobClient.sendBlobsToFilestore(blobs);
688
+ this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
689
+ ...proposalInfo,
690
+ numBlobs: blobs.length,
691
+ });
692
+ } catch (err) {
693
+ this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
694
+ }
339
695
  }
340
696
 
341
697
  private slashInvalidBlock(proposal: BlockProposal) {
@@ -360,37 +716,62 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
360
716
  validator: proposer,
361
717
  amount: this.config.slashBroadcastedInvalidBlockPenalty,
362
718
  offenseType: OffenseType.BROADCASTED_INVALID_BLOCK_PROPOSAL,
363
- epochOrSlot: proposal.slotNumber.toBigInt(),
719
+ epochOrSlot: BigInt(proposal.slotNumber),
364
720
  },
365
721
  ]);
366
722
  }
367
723
 
368
724
  async createBlockProposal(
369
- blockNumber: number,
370
- header: CheckpointHeader,
725
+ blockHeader: BlockHeader,
726
+ indexWithinCheckpoint: IndexWithinCheckpoint,
727
+ inHash: Fr,
371
728
  archive: Fr,
372
- stateReference: StateReference,
373
729
  txs: Tx[],
374
730
  proposerAddress: EthAddress | undefined,
375
- options: BlockProposalOptions,
376
- ): Promise<BlockProposal | undefined> {
377
- if (this.previousProposal?.slotNumber.equals(header.slotNumber)) {
378
- this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
379
- return Promise.resolve(undefined);
380
- }
381
-
731
+ options: BlockProposalOptions = {},
732
+ ): Promise<BlockProposal> {
733
+ // TODO(palla/mbps): Prevent double proposals properly
734
+ // if (this.previousProposal?.slotNumber === blockHeader.globalVariables.slotNumber) {
735
+ // this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
736
+ // return Promise.resolve(undefined);
737
+ // }
738
+
739
+ this.log.info(
740
+ `Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`,
741
+ );
382
742
  const newProposal = await this.validationService.createBlockProposal(
383
- header,
743
+ blockHeader,
744
+ indexWithinCheckpoint,
745
+ inHash,
384
746
  archive,
385
- stateReference,
386
747
  txs,
387
748
  proposerAddress,
388
- { ...options, broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal },
749
+ {
750
+ ...options,
751
+ broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal,
752
+ },
389
753
  );
390
754
  this.previousProposal = newProposal;
391
755
  return newProposal;
392
756
  }
393
757
 
758
+ async createCheckpointProposal(
759
+ checkpointHeader: CheckpointHeader,
760
+ archive: Fr,
761
+ lastBlockInfo: CreateCheckpointProposalLastBlockData | undefined,
762
+ proposerAddress: EthAddress | undefined,
763
+ options: CheckpointProposalOptions = {},
764
+ ): Promise<CheckpointProposal> {
765
+ this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
766
+ return await this.validationService.createCheckpointProposal(
767
+ checkpointHeader,
768
+ archive,
769
+ lastBlockInfo,
770
+ proposerAddress,
771
+ options,
772
+ );
773
+ }
774
+
394
775
  async broadcastBlockProposal(proposal: BlockProposal): Promise<void> {
395
776
  await this.p2pClient.broadcastProposal(proposal);
396
777
  }
@@ -398,20 +779,34 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
398
779
  async signAttestationsAndSigners(
399
780
  attestationsAndSigners: CommitteeAttestationsAndSigners,
400
781
  proposer: EthAddress,
782
+ slot: SlotNumber,
783
+ blockNumber: BlockNumber | CheckpointNumber,
401
784
  ): Promise<Signature> {
402
- return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
785
+ return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber);
403
786
  }
404
787
 
405
- async collectOwnAttestations(proposal: BlockProposal): Promise<BlockAttestation[]> {
406
- const slot = proposal.payload.header.slotNumber.toBigInt();
788
+ async collectOwnAttestations(proposal: CheckpointProposal): Promise<CheckpointAttestation[]> {
789
+ const slot = proposal.slotNumber;
407
790
  const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
408
791
  this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, { inCommittee });
409
- return this.createBlockAttestationsFromProposal(proposal, inCommittee);
792
+ const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
793
+
794
+ // We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
795
+ // other nodes can see that our validators did attest to this block proposal, and do not slash us
796
+ // due to inactivity for missed attestations.
797
+ void this.p2pClient.broadcastCheckpointAttestations(attestations).catch(err => {
798
+ this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
799
+ });
800
+ return attestations;
410
801
  }
411
802
 
412
- async collectAttestations(proposal: BlockProposal, required: number, deadline: Date): Promise<BlockAttestation[]> {
413
- // Wait and poll the p2pClient's attestation pool for this block until we have enough attestations
414
- const slot = proposal.payload.header.slotNumber.toBigInt();
803
+ async collectAttestations(
804
+ proposal: CheckpointProposal,
805
+ required: number,
806
+ deadline: Date,
807
+ ): Promise<CheckpointAttestation[]> {
808
+ // Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
809
+ const slot = proposal.slotNumber;
415
810
  this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
416
811
 
417
812
  if (+deadline < this.dateProvider.now()) {
@@ -426,16 +821,16 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
426
821
  const proposalId = proposal.archive.toString();
427
822
  const myAddresses = this.getValidatorAddresses();
428
823
 
429
- let attestations: BlockAttestation[] = [];
824
+ let attestations: CheckpointAttestation[] = [];
430
825
  while (true) {
431
- // Filter out attestations with a mismatching payload. This should NOT happen since we have verified
826
+ // Filter out attestations with a mismatching archive. This should NOT happen since we have verified
432
827
  // the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
433
- const collectedAttestations = (await this.p2pClient.getAttestationsForSlot(slot, proposalId)).filter(
828
+ const collectedAttestations = (await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalId)).filter(
434
829
  attestation => {
435
- if (!attestation.payload.equals(proposal.payload)) {
830
+ if (!attestation.archive.equals(proposal.archive)) {
436
831
  this.log.warn(
437
- `Received attestation for slot ${slot} with mismatched payload from ${attestation.getSender()?.toString()}`,
438
- { attestationPayload: attestation.payload, proposalPayload: proposal.payload },
832
+ `Received attestation for slot ${slot} with mismatched archive from ${attestation.getSender()?.toString()}`,
833
+ { attestationArchive: attestation.archive.toString(), proposalArchive: proposal.archive.toString() },
439
834
  );
440
835
  return false;
441
836
  }
@@ -476,15 +871,6 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
476
871
  }
477
872
  }
478
873
 
479
- private async createBlockAttestationsFromProposal(
480
- proposal: BlockProposal,
481
- attestors: EthAddress[] = [],
482
- ): Promise<BlockAttestation[]> {
483
- const attestations = await this.validationService.attestToProposal(proposal, attestors);
484
- await this.p2pClient.addAttestations(attestations);
485
- return attestations;
486
- }
487
-
488
874
  private async handleAuthRequest(peer: PeerId, msg: Buffer): Promise<Buffer> {
489
875
  const authRequest = AuthRequest.fromBuffer(msg);
490
876
  const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch(_ => undefined);
@@ -503,7 +889,9 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
503
889
  }
504
890
 
505
891
  const payloadToSign = authRequest.getPayloadToSign();
506
- const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign);
892
+ // AUTH_REQUEST doesn't require HA protection - multiple signatures are safe
893
+ const context: SigningContext = { dutyType: DutyType.AUTH_REQUEST };
894
+ const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign, context);
507
895
  const authResponse = new AuthResponse(statusMessage, signature);
508
896
  return authResponse.toBuffer();
509
897
  }