@aztec/validator-client 0.0.1-commit.d3ec352c → 0.0.1-commit.d431d1c

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