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

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