@aztec/validator-client 0.0.0-test.1 → 0.0.1-commit.017a351

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 (66) hide show
  1. package/README.md +325 -0
  2. package/dest/checkpoint_builder.d.ts +79 -0
  3. package/dest/checkpoint_builder.d.ts.map +1 -0
  4. package/dest/checkpoint_builder.js +251 -0
  5. package/dest/config.d.ts +3 -14
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +75 -12
  8. package/dest/duties/validation_service.d.ts +49 -13
  9. package/dest/duties/validation_service.d.ts.map +1 -1
  10. package/dest/duties/validation_service.js +112 -18
  11. package/dest/factory.d.ts +34 -6
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +19 -6
  14. package/dest/index.d.ts +5 -2
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +4 -1
  17. package/dest/key_store/ha_key_store.d.ts +99 -0
  18. package/dest/key_store/ha_key_store.d.ts.map +1 -0
  19. package/dest/key_store/ha_key_store.js +208 -0
  20. package/dest/key_store/index.d.ts +4 -1
  21. package/dest/key_store/index.d.ts.map +1 -1
  22. package/dest/key_store/index.js +3 -0
  23. package/dest/key_store/interface.d.ts +85 -6
  24. package/dest/key_store/interface.d.ts.map +1 -1
  25. package/dest/key_store/interface.js +3 -3
  26. package/dest/key_store/local_key_store.d.ts +46 -11
  27. package/dest/key_store/local_key_store.d.ts.map +1 -1
  28. package/dest/key_store/local_key_store.js +68 -17
  29. package/dest/key_store/node_keystore_adapter.d.ts +151 -0
  30. package/dest/key_store/node_keystore_adapter.d.ts.map +1 -0
  31. package/dest/key_store/node_keystore_adapter.js +330 -0
  32. package/dest/key_store/web3signer_key_store.d.ts +66 -0
  33. package/dest/key_store/web3signer_key_store.d.ts.map +1 -0
  34. package/dest/key_store/web3signer_key_store.js +156 -0
  35. package/dest/metrics.d.ts +25 -5
  36. package/dest/metrics.d.ts.map +1 -1
  37. package/dest/metrics.js +86 -21
  38. package/dest/proposal_handler.d.ts +134 -0
  39. package/dest/proposal_handler.d.ts.map +1 -0
  40. package/dest/proposal_handler.js +1072 -0
  41. package/dest/validator.d.ts +109 -59
  42. package/dest/validator.d.ts.map +1 -1
  43. package/dest/validator.js +698 -171
  44. package/package.json +37 -21
  45. package/src/checkpoint_builder.ts +417 -0
  46. package/src/config.ts +87 -26
  47. package/src/duties/validation_service.ts +200 -21
  48. package/src/factory.ts +82 -11
  49. package/src/index.ts +4 -1
  50. package/src/key_store/ha_key_store.ts +269 -0
  51. package/src/key_store/index.ts +3 -0
  52. package/src/key_store/interface.ts +100 -5
  53. package/src/key_store/local_key_store.ts +77 -18
  54. package/src/key_store/node_keystore_adapter.ts +398 -0
  55. package/src/key_store/web3signer_key_store.ts +205 -0
  56. package/src/metrics.ts +121 -22
  57. package/src/proposal_handler.ts +1161 -0
  58. package/src/validator.ts +975 -222
  59. package/dest/errors/index.d.ts +0 -2
  60. package/dest/errors/index.d.ts.map +0 -1
  61. package/dest/errors/index.js +0 -1
  62. package/dest/errors/validator.error.d.ts +0 -29
  63. package/dest/errors/validator.error.d.ts.map +0 -1
  64. package/dest/errors/validator.error.js +0 -45
  65. package/src/errors/index.ts +0 -1
  66. package/src/errors/validator.error.ts +0 -55
package/src/validator.ts CHANGED
@@ -1,337 +1,1073 @@
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 { Buffer32 } from '@aztec/foundation/buffer';
3
- import type { Fr } from '@aztec/foundation/fields';
4
- import { createLogger } from '@aztec/foundation/log';
4
+ import { CheckpointNumber, EpochNumber, IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
5
+ import { Fr } from '@aztec/foundation/curves/bn254';
6
+ import type { EthAddress } from '@aztec/foundation/eth-address';
7
+ import type { Signature } from '@aztec/foundation/eth-signature';
8
+ import { FifoSet } from '@aztec/foundation/fifo-set';
9
+ import { type LogData, type Logger, createLogger } from '@aztec/foundation/log';
5
10
  import { RunningPromise } from '@aztec/foundation/running-promise';
6
11
  import { sleep } from '@aztec/foundation/sleep';
7
- import { DateProvider, type Timer } from '@aztec/foundation/timer';
8
- import type { P2P } from '@aztec/p2p';
9
- import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
10
- import type { L2Block } from '@aztec/stdlib/block';
11
- import type { BlockAttestation, BlockProposal } from '@aztec/stdlib/p2p';
12
- import type { BlockHeader, GlobalVariables, Tx, TxHash } from '@aztec/stdlib/tx';
13
- import { type TelemetryClient, WithTracer, getTelemetryClient } from '@aztec/telemetry-client';
14
-
15
- import type { ValidatorClientConfig } from './config.js';
16
- import { ValidationService } from './duties/validation_service.js';
12
+ import { DateProvider } from '@aztec/foundation/timer';
13
+ import type { KeystoreManager } from '@aztec/node-keystore';
14
+ import type { DuplicateAttestationInfo, DuplicateProposalInfo, P2P, PeerId } from '@aztec/p2p';
15
+ import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
16
+ import {
17
+ OffenseType,
18
+ WANT_TO_CLEAR_SLASH_EVENT,
19
+ WANT_TO_SLASH_EVENT,
20
+ type Watcher,
21
+ type WatcherEmitter,
22
+ getOffenseTypeName,
23
+ } from '@aztec/slasher';
24
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
25
+ import type { CommitteeAttestationsAndSigners, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
26
+ import type { CheckpointReexecutionTracker } from '@aztec/stdlib/checkpoint';
27
+ import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
28
+ import type {
29
+ ITxProvider,
30
+ Validator,
31
+ ValidatorClientFullConfig,
32
+ WorldStateSynchronizer,
33
+ } from '@aztec/stdlib/interfaces/server';
34
+ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
17
35
  import {
18
- AttestationTimeoutError,
19
- BlockBuilderNotProvidedError,
20
- InvalidValidatorPrivateKeyError,
21
- ReExFailedTxsError,
22
- ReExStateMismatchError,
23
- ReExTimeoutError,
24
- TransactionsNotAvailableError,
25
- } from './errors/validator.error.js';
26
- import type { ValidatorKeyStore } from './key_store/interface.js';
27
- import { LocalKeyStore } from './key_store/local_key_store.js';
36
+ type BlockProposal,
37
+ type BlockProposalOptions,
38
+ type CheckpointAttestation,
39
+ CheckpointProposal,
40
+ type CheckpointProposalCore,
41
+ type CheckpointProposalOptions,
42
+ type CoordinationSignatureContext,
43
+ } from '@aztec/stdlib/p2p';
44
+ import type { CheckpointHeader } from '@aztec/stdlib/rollup';
45
+ import type { BlockHeader, Tx } from '@aztec/stdlib/tx';
46
+ import { AttestationTimeoutError } from '@aztec/stdlib/validators';
47
+ import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
48
+ import {
49
+ createHASigner,
50
+ createLocalSignerWithProtection,
51
+ createSignerFromSharedDb,
52
+ } from '@aztec/validator-ha-signer/factory';
53
+ import { DutyType, type SigningContext, type SlashingProtectionDatabase } from '@aztec/validator-ha-signer/types';
54
+ import type { ValidatorHASigner } from '@aztec/validator-ha-signer/validator-ha-signer';
55
+
56
+ import { EventEmitter } from 'events';
57
+ import type { TypedDataDefinition } from 'viem';
58
+
59
+ import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
60
+ import { ValidationService } from './duties/validation_service.js';
61
+ import { HAKeyStore } from './key_store/ha_key_store.js';
62
+ import type { ExtendedValidatorKeyStore } from './key_store/interface.js';
63
+ import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
28
64
  import { ValidatorMetrics } from './metrics.js';
65
+ import {
66
+ type BlockProposalValidationFailureReason,
67
+ type CheckpointProposalValidationFailureReason,
68
+ type CheckpointProposalValidationFailureResult,
69
+ ProposalHandler,
70
+ } from './proposal_handler.js';
29
71
 
30
- /**
31
- * Callback function for building a block
32
- *
33
- * We reuse the sequencer's block building functionality for re-execution
34
- */
35
- type BlockBuilderCallback = (
36
- txs: Iterable<Tx> | AsyncIterableIterator<Tx>,
37
- globalVariables: GlobalVariables,
38
- opts?: { validateOnly?: boolean },
39
- ) => Promise<{
40
- block: L2Block;
41
- publicProcessorDuration: number;
42
- numTxs: number;
43
- numFailedTxs: number;
44
- blockBuildingTimer: Timer;
45
- }>;
46
-
47
- export interface Validator {
48
- start(): Promise<void>;
49
- registerBlockProposalHandler(): void;
50
- registerBlockBuilder(blockBuilder: BlockBuilderCallback): void;
51
-
52
- // Block validation responsibilities
53
- createBlockProposal(header: BlockHeader, archive: Fr, txs: TxHash[]): Promise<BlockProposal | undefined>;
54
- attestToProposal(proposal: BlockProposal): void;
55
-
56
- broadcastBlockProposal(proposal: BlockProposal): void;
57
- collectAttestations(proposal: BlockProposal, required: number, deadline: Date): Promise<BlockAttestation[]>;
58
- }
72
+ // We maintain a set of proposers who have proposed invalid blocks.
73
+ // Just cap the set to avoid unbounded growth.
74
+ const MAX_PROPOSERS_OF_INVALID_BLOCKS = 1000;
75
+ const MAX_TRACKED_INVALID_PROPOSAL_SLOTS = 1000;
76
+ const MAX_TRACKED_INVALID_CHECKPOINT_PROPOSALS = 1000;
77
+ const MAX_TRACKED_BAD_ATTESTATIONS = 10_000;
78
+
79
+ // What errors from the block proposal handler result in slashing
80
+ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT: BlockProposalValidationFailureReason[] = [
81
+ 'state_mismatch',
82
+ 'failed_txs',
83
+ 'global_variables_mismatch',
84
+ 'invalid_proposal',
85
+ 'parent_block_wrong_slot',
86
+ 'in_hash_mismatch',
87
+ ];
88
+
89
+ const SLASHABLE_CHECKPOINT_PROPOSAL_VALIDATION_RESULT: Record<CheckpointProposalValidationFailureReason, boolean> = {
90
+ // enabled
91
+ ['invalid_fee_asset_price_modifier']: true,
92
+ ['checkpoint_header_mismatch']: true,
93
+ // These late mismatches should normally be caught by earlier checks, but if reached after validating the local
94
+ // checkpoint inputs, the proposer-signed payload disagrees with deterministic recomputation.
95
+ ['archive_mismatch']: true,
96
+ ['out_hash_mismatch']: true,
97
+ ['no_blocks_for_slot']: true,
98
+ ['too_many_blocks_in_checkpoint']: true,
99
+ ['checkpoint_validation_failed']: true,
100
+ ['last_block_archive_mismatch']: true,
101
+
102
+ // disabled
103
+ ['invalid_signature']: false,
104
+ ['last_block_not_found']: false,
105
+ ['block_fetch_error']: false,
106
+ ['checkpoint_already_published']: false,
107
+ };
59
108
 
60
109
  /**
61
110
  * Validator Client
62
111
  */
63
- export class ValidatorClient extends WithTracer implements Validator {
112
+ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter) implements Validator, Watcher {
113
+ public readonly tracer: Tracer;
64
114
  private validationService: ValidationService;
65
115
  private metrics: ValidatorMetrics;
116
+ private log: Logger;
117
+ // Whether it has already registered handlers on the p2p client
118
+ private hasRegisteredHandlers = false;
66
119
 
67
- // Used to check if we are sending the same proposal twice
68
- private previousProposal?: BlockProposal;
120
+ /** Tracks the last block proposal we created, to detect duplicate proposal attempts. */
121
+ private lastProposedBlock?: BlockProposal;
69
122
 
70
- // Callback registered to: sequencer.buildBlock
71
- private blockBuilder?: BlockBuilderCallback = undefined;
123
+ /** Tracks the last checkpoint proposal we created. */
124
+ private lastProposedCheckpoint?: CheckpointProposal;
72
125
 
126
+ private lastEpochForCommitteeUpdateLoop: EpochNumber | undefined;
73
127
  private epochCacheUpdateLoop: RunningPromise;
128
+ /** Tracks the last epoch in which each attester successfully submitted at least one attestation. */
129
+ private lastAttestedEpochByAttester: Map<string, EpochNumber> = new Map();
130
+
131
+ private proposersOfInvalidBlocks = FifoSet.withLimit<string>(MAX_PROPOSERS_OF_INVALID_BLOCKS);
132
+ private slotsWithInvalidProposals = FifoSet.withLimit<SlotNumber>(MAX_TRACKED_INVALID_PROPOSAL_SLOTS);
133
+ private invalidCheckpointProposalOffenseKeys = FifoSet.withLimit<string>(MAX_TRACKED_INVALID_CHECKPOINT_PROPOSALS);
134
+ private badAttestationOffenseKeys = FifoSet.withLimit<string>(MAX_TRACKED_BAD_ATTESTATIONS);
135
+ private slotsWithProposalEquivocation = FifoSet.withLimit<SlotNumber>(MAX_TRACKED_INVALID_PROPOSAL_SLOTS);
74
136
 
75
- private blockProposalValidator: BlockProposalValidator;
137
+ /** Tracks the last checkpoint proposal we attested to, to prevent equivocation. */
138
+ private lastAttestedProposal?: CheckpointProposalCore;
76
139
 
77
- constructor(
78
- private keyStore: ValidatorKeyStore,
140
+ protected constructor(
141
+ private keyStore: ExtendedValidatorKeyStore,
79
142
  private epochCache: EpochCache,
80
143
  private p2pClient: P2P,
81
- private config: ValidatorClientConfig,
144
+ private proposalHandler: ProposalHandler,
145
+ private blockSource: L2BlockSource,
146
+ private checkpointsBuilder: FullNodeCheckpointsBuilder,
147
+ private worldState: WorldStateSynchronizer,
148
+ private l1ToL2MessageSource: L1ToL2MessageSource,
149
+ private config: ValidatorClientFullConfig,
150
+ private blobClient: BlobClientInterface,
151
+ private slashingProtectionSigner: ValidatorHASigner,
82
152
  private dateProvider: DateProvider = new DateProvider(),
83
153
  telemetry: TelemetryClient = getTelemetryClient(),
84
- private log = createLogger('validator'),
154
+ log = createLogger('validator'),
85
155
  ) {
86
- // Instantiate tracer
87
- super(telemetry, 'Validator');
88
- this.metrics = new ValidatorMetrics(telemetry);
156
+ super();
89
157
 
90
- this.validationService = new ValidationService(keyStore);
158
+ // Create child logger with fisherman prefix if in fisherman mode
159
+ this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
91
160
 
92
- this.blockProposalValidator = new BlockProposalValidator(epochCache);
161
+ this.tracer = telemetry.getTracer('Validator');
162
+ this.metrics = new ValidatorMetrics(telemetry);
93
163
 
94
- // Refresh epoch cache every second to trigger commiteeChanged event
95
- this.epochCacheUpdateLoop = new RunningPromise(
96
- () =>
97
- this.epochCache
98
- .getCommittee()
99
- .then(() => {})
100
- .catch(err => log.error('Error updating validator committee', err)),
101
- log,
102
- 1000,
164
+ this.validationService = new ValidationService(
165
+ keyStore,
166
+ this.getSignatureContext(),
167
+ this.log.createChild('validation-service'),
168
+ );
169
+ this.proposalHandler.setCheckpointProposalValidationFailureCallback((proposal, result, proposalInfo) =>
170
+ this.handleInvalidCheckpointProposal(proposal, result, proposalInfo),
103
171
  );
104
172
 
105
- // Listen to commiteeChanged event to alert operator when their validator has entered the committee
106
- this.epochCache.on('committeeChanged', (newCommittee, epochNumber) => {
107
- const me = this.keyStore.getAddress();
108
- if (newCommittee.some(addr => addr.equals(me))) {
109
- this.log.info(`Validator ${me.toString()} is on the validator committee for epoch ${epochNumber}`);
110
- } else {
111
- this.log.verbose(`Validator ${me.toString()} not on the validator committee for epoch ${epochNumber}`);
173
+ // Refresh epoch cache every second to trigger alert if participation in committee changes
174
+ this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), this.log, 1000);
175
+ const myAddresses = this.getValidatorAddresses();
176
+ this.log.verbose(`Initialized validator with addresses: ${myAddresses.map(a => a.toString()).join(', ')}`);
177
+ }
178
+
179
+ public static validateKeyStoreConfiguration(keyStoreManager: KeystoreManager, logger?: Logger) {
180
+ const validatorKeyStore = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
181
+ const validatorAddresses = validatorKeyStore.getAddresses();
182
+ // Verify that we can retrieve all required data from the key store
183
+ for (const address of validatorAddresses) {
184
+ // Functions throw if required data is not available
185
+ let coinbase: EthAddress;
186
+ let feeRecipient: AztecAddress;
187
+ try {
188
+ coinbase = validatorKeyStore.getCoinbaseAddress(address);
189
+ feeRecipient = validatorKeyStore.getFeeRecipient(address);
190
+ } catch (error) {
191
+ throw new Error(`Failed to retrieve required data for validator address ${address}, error: ${error}`);
112
192
  }
113
- });
114
193
 
115
- this.log.verbose(`Initialized validator with address ${this.keyStore.getAddress().toString()}`);
194
+ const publisherAddresses = validatorKeyStore.getPublisherAddresses(address);
195
+ if (!publisherAddresses.length) {
196
+ throw new Error(`No publisher addresses found for validator address ${address}`);
197
+ }
198
+ logger?.debug(
199
+ `Validator ${address.toString()} configured with coinbase ${coinbase.toString()}, feeRecipient ${feeRecipient.toString()} and publishers ${publisherAddresses.map(x => x.toString()).join()}`,
200
+ );
201
+ }
116
202
  }
117
203
 
118
- static new(
119
- config: ValidatorClientConfig,
204
+ private async handleEpochCommitteeUpdate() {
205
+ try {
206
+ const { committee, epoch } = await this.epochCache.getCommittee('next');
207
+ if (!committee) {
208
+ this.log.trace(`No committee found for slot`);
209
+ return;
210
+ }
211
+ this.metrics.setCurrentEpoch(epoch);
212
+ if (epoch !== this.lastEpochForCommitteeUpdateLoop) {
213
+ const me = this.getValidatorAddresses();
214
+ const committeeSet = new Set(committee.map(v => v.toString()));
215
+ const inCommittee = me.filter(a => committeeSet.has(a.toString()));
216
+ if (inCommittee.length > 0) {
217
+ this.log.info(
218
+ `Validators ${inCommittee.map(a => a.toString()).join(',')} are on the validator committee for epoch ${epoch}`,
219
+ );
220
+ } else {
221
+ this.log.verbose(
222
+ `Validators ${me.map(a => a.toString()).join(', ')} are not on the validator committee for epoch ${epoch}`,
223
+ );
224
+ }
225
+ this.lastEpochForCommitteeUpdateLoop = epoch;
226
+ }
227
+ } catch (err) {
228
+ this.log.error(`Error updating epoch committee`, err);
229
+ }
230
+ }
231
+
232
+ static async new(
233
+ config: ValidatorClientFullConfig,
234
+ checkpointsBuilder: FullNodeCheckpointsBuilder,
235
+ worldState: WorldStateSynchronizer,
120
236
  epochCache: EpochCache,
121
237
  p2pClient: P2P,
238
+ blockSource: L2BlockSource & L2BlockSink,
239
+ l1ToL2MessageSource: L1ToL2MessageSource,
240
+ txProvider: ITxProvider,
241
+ keyStoreManager: KeystoreManager,
242
+ blobClient: BlobClientInterface,
243
+ reexecutionTracker: CheckpointReexecutionTracker,
122
244
  dateProvider: DateProvider = new DateProvider(),
123
245
  telemetry: TelemetryClient = getTelemetryClient(),
246
+ slashingProtectionDb?: SlashingProtectionDatabase,
124
247
  ) {
125
- if (!config.validatorPrivateKey) {
126
- throw new InvalidValidatorPrivateKeyError();
248
+ const metrics = new ValidatorMetrics(telemetry);
249
+ const blockProposalValidator = new BlockProposalValidator(epochCache, {
250
+ txsPermitted: !config.disableTransactions,
251
+ maxTxsPerBlock: config.validateMaxTxsPerBlock,
252
+ maxBlocksPerCheckpoint: config.maxBlocksPerCheckpoint,
253
+ skipSlotValidation: config.skipProposalSlotValidation,
254
+ signatureContext: {
255
+ chainId: config.l1ChainId,
256
+ rollupAddress: config.rollupAddress,
257
+ },
258
+ });
259
+ const proposalHandler = new ProposalHandler(
260
+ checkpointsBuilder,
261
+ worldState,
262
+ blockSource,
263
+ l1ToL2MessageSource,
264
+ txProvider,
265
+ blockProposalValidator,
266
+ epochCache,
267
+ config,
268
+ blobClient,
269
+ reexecutionTracker,
270
+ metrics,
271
+ dateProvider,
272
+ telemetry,
273
+ undefined,
274
+ );
275
+
276
+ const nodeKeystoreAdapter = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
277
+ let slashingProtectionSigner: ValidatorHASigner;
278
+ if (slashingProtectionDb) {
279
+ // Shared database mode: use a pre-existing database (e.g. for testing HA setups).
280
+ ({ signer: slashingProtectionSigner } = createSignerFromSharedDb(slashingProtectionDb, config, {
281
+ telemetryClient: telemetry,
282
+ dateProvider,
283
+ }));
284
+ } else if (config.haSigningEnabled) {
285
+ // Multi-node HA mode: use PostgreSQL-backed distributed locking.
286
+ // If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
287
+ const haConfig = {
288
+ ...config,
289
+ maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000,
290
+ };
291
+ ({ signer: slashingProtectionSigner } = await createHASigner(haConfig, {
292
+ telemetryClient: telemetry,
293
+ dateProvider,
294
+ }));
295
+ } else {
296
+ // Single-node mode: use LMDB-backed local signing protection.
297
+ // This prevents double-signing if the node crashes and restarts mid-proposal.
298
+ ({ signer: slashingProtectionSigner } = await createLocalSignerWithProtection(config, {
299
+ telemetryClient: telemetry,
300
+ dateProvider,
301
+ }));
127
302
  }
303
+ const validatorKeyStore: ExtendedValidatorKeyStore = new HAKeyStore(nodeKeystoreAdapter, slashingProtectionSigner);
128
304
 
129
- const privateKey = validatePrivateKey(config.validatorPrivateKey);
130
- const localKeyStore = new LocalKeyStore(privateKey);
305
+ const validator = new ValidatorClient(
306
+ validatorKeyStore,
307
+ epochCache,
308
+ p2pClient,
309
+ proposalHandler,
310
+ blockSource,
311
+ checkpointsBuilder,
312
+ worldState,
313
+ l1ToL2MessageSource,
314
+ config,
315
+ blobClient,
316
+ slashingProtectionSigner,
317
+ dateProvider,
318
+ telemetry,
319
+ );
131
320
 
132
- const validator = new ValidatorClient(localKeyStore, epochCache, p2pClient, config, dateProvider, telemetry);
133
- validator.registerBlockProposalHandler();
134
321
  return validator;
135
322
  }
136
323
 
324
+ public getValidatorAddresses() {
325
+ return this.keyStore
326
+ .getAddresses()
327
+ .filter(addr => !this.config.disabledValidators.some(disabled => disabled.equals(addr)));
328
+ }
329
+
330
+ public getProposalHandler() {
331
+ return this.proposalHandler;
332
+ }
333
+
334
+ public signWithAddress(addr: EthAddress, msg: TypedDataDefinition, context: SigningContext) {
335
+ return this.keyStore.signTypedDataWithAddress(addr, msg, context);
336
+ }
337
+
338
+ private getSignatureContext(): CoordinationSignatureContext {
339
+ return {
340
+ chainId: this.config.l1ChainId,
341
+ rollupAddress: this.config.rollupAddress,
342
+ };
343
+ }
344
+
345
+ public getCoinbaseForAttestor(attestor: EthAddress): EthAddress {
346
+ return this.keyStore.getCoinbaseAddress(attestor);
347
+ }
348
+
349
+ public getFeeRecipientForAttestor(attestor: EthAddress): AztecAddress {
350
+ return this.keyStore.getFeeRecipient(attestor);
351
+ }
352
+
353
+ public getConfig(): ValidatorClientFullConfig {
354
+ return this.config;
355
+ }
356
+
357
+ public hasProposalEquivocation(slotNumber: SlotNumber): boolean {
358
+ return this.slotsWithProposalEquivocation.has(slotNumber);
359
+ }
360
+
361
+ public hasInvalidProposals(slotNumber: SlotNumber): boolean {
362
+ return this.slotsWithInvalidProposals.has(slotNumber);
363
+ }
364
+
365
+ public updateConfig(config: Partial<ValidatorClientFullConfig>) {
366
+ this.config = { ...this.config, ...config };
367
+ this.proposalHandler.updateConfig(config);
368
+ }
369
+
370
+ public reloadKeystore(newManager: KeystoreManager): void {
371
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
372
+ this.keyStore = new HAKeyStore(newAdapter, this.slashingProtectionSigner);
373
+ this.validationService = new ValidationService(
374
+ this.keyStore,
375
+ this.getSignatureContext(),
376
+ this.log.createChild('validation-service'),
377
+ );
378
+ }
379
+
137
380
  public async start() {
138
- // Sync the committee from the smart contract
139
- // https://github.com/AztecProtocol/aztec-packages/issues/7962
381
+ if (this.epochCacheUpdateLoop.isRunning()) {
382
+ this.log.warn(`Validator client already started`);
383
+ return;
384
+ }
140
385
 
141
- const me = this.keyStore.getAddress();
142
- const inCommittee = await this.epochCache.isInCommittee(me);
143
- if (inCommittee) {
144
- this.log.info(`Started validator with address ${me.toString()} in current validator committee`);
145
- } else {
146
- this.log.info(`Started validator with address ${me.toString()}`);
386
+ await this.keyStore.start();
387
+
388
+ await this.registerHandlers();
389
+
390
+ const myAddresses = this.getValidatorAddresses();
391
+ const inCommittee = await this.epochCache.filterInCommittee('now', myAddresses);
392
+ this.log.info(`Started validator with addresses: ${myAddresses.map(a => a.toString()).join(', ')}`);
393
+ if (inCommittee.length > 0) {
394
+ this.log.info(`Addresses in current validator committee: ${inCommittee.map(a => a.toString()).join(', ')}`);
147
395
  }
148
396
  this.epochCacheUpdateLoop.start();
397
+
149
398
  return Promise.resolve();
150
399
  }
151
400
 
152
401
  public async stop() {
153
402
  await this.epochCacheUpdateLoop.stop();
403
+ await this.keyStore.stop();
154
404
  }
155
405
 
156
- public registerBlockProposalHandler() {
157
- const handler = (block: BlockProposal): Promise<BlockAttestation | undefined> => {
158
- return this.attestToProposal(block);
159
- };
160
- this.p2pClient.registerBlockProposalHandler(handler);
406
+ /** Register handlers on the p2p client */
407
+ public async registerHandlers() {
408
+ if (!this.hasRegisteredHandlers) {
409
+ this.hasRegisteredHandlers = true;
410
+ this.log.debug(`Registering validator handlers for p2p client`);
411
+
412
+ // Block proposal handler - validates but does NOT attest (validators only attest to checkpoints)
413
+ const blockHandler = (block: BlockProposal, proposalSender: PeerId): Promise<boolean> =>
414
+ this.validateBlockProposal(block, proposalSender);
415
+ this.p2pClient.registerBlockProposalHandler(blockHandler);
416
+
417
+ // Checkpoint proposal handler - validates and creates attestations
418
+ // The checkpoint is received as CheckpointProposalCore since the lastBlock is extracted
419
+ // and processed separately via the block handler above.
420
+ const checkpointHandler = (
421
+ checkpoint: CheckpointProposalCore,
422
+ proposalSender: PeerId,
423
+ ): Promise<CheckpointAttestation[] | undefined> => this.attestToCheckpointProposal(checkpoint, proposalSender);
424
+ this.p2pClient.registerValidatorCheckpointProposalHandler(checkpointHandler);
425
+
426
+ // Duplicate proposal handler - triggers slashing for equivocation
427
+ this.p2pClient.registerDuplicateProposalCallback((info: DuplicateProposalInfo) => {
428
+ this.handleDuplicateProposal(info);
429
+ });
430
+
431
+ // Duplicate attestation handler - triggers slashing for attestation equivocation
432
+ this.p2pClient.registerDuplicateAttestationCallback((info: DuplicateAttestationInfo) => {
433
+ this.handleDuplicateAttestation(info);
434
+ });
435
+
436
+ this.p2pClient.registerCheckpointAttestationCallback((attestation: CheckpointAttestation) => {
437
+ this.handleCheckpointAttestation(attestation);
438
+ });
439
+
440
+ const myAddresses = this.getValidatorAddresses();
441
+ this.p2pClient.registerThisValidatorAddresses(myAddresses);
442
+
443
+ await this.p2pClient.addReqRespSubProtocol(ReqRespSubProtocol.AUTH, this.handleAuthRequest.bind(this));
444
+ }
161
445
  }
162
446
 
163
447
  /**
164
- * Register a callback function for building a block
165
- *
166
- * We reuse the sequencer's block building functionality for re-execution
448
+ * Validate a block proposal from a peer.
449
+ * Note: Validators do NOT attest to individual blocks - attestations are only for checkpoint proposals.
450
+ * @returns true if the proposal is valid, false otherwise
167
451
  */
168
- public registerBlockBuilder(blockBuilder: BlockBuilderCallback) {
169
- this.blockBuilder = blockBuilder;
452
+ async validateBlockProposal(proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> {
453
+ const slotNumber = proposal.slotNumber;
454
+
455
+ // Note: During escape hatch, we still want to "validate" proposals for observability,
456
+ // but we intentionally reject them and disable slashing invalid block and attestation flow.
457
+ const escapeHatchOpen = await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber);
458
+
459
+ const proposer = proposal.getSender();
460
+
461
+ // Reject proposals with invalid signatures
462
+ if (!proposer) {
463
+ this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
464
+ return false;
465
+ }
466
+
467
+ // Log self-proposals from HA peers (same validator key on different nodes)
468
+ if (this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
469
+ this.log.verbose(`Processing block proposal from HA peer for slot ${slotNumber}`, {
470
+ proposer: proposer.toString(),
471
+ slotNumber,
472
+ });
473
+ }
474
+
475
+ // Check if we're in the committee (for metrics purposes)
476
+ const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
477
+ const partOfCommittee = inCommittee.length > 0;
478
+
479
+ const proposalInfo = { ...proposal.toBlockInfo(), proposer: proposer.toString() };
480
+ this.log.info(`Received block proposal for slot ${slotNumber}`, {
481
+ ...proposalInfo,
482
+ txHashes: proposal.txHashes.map(t => t.toString()),
483
+ fishermanMode: this.config.fishermanMode || false,
484
+ });
485
+
486
+ // Reexecute outside the escape hatch so slashing observers can detect invalid proposals even when penalties are 0.
487
+ const validationResult = await this.proposalHandler.handleBlockProposal(proposal, proposalSender, !escapeHatchOpen);
488
+
489
+ if (!validationResult.isValid) {
490
+ const reason = validationResult.reason || 'unknown';
491
+
492
+ this.log.warn(`Block proposal validation failed: ${reason}`, proposalInfo);
493
+
494
+ // Classify failure reason: bad proposal vs node issue
495
+ const badProposalReasons: BlockProposalValidationFailureReason[] = [
496
+ 'invalid_proposal',
497
+ 'state_mismatch',
498
+ 'failed_txs',
499
+ 'in_hash_mismatch',
500
+ 'parent_block_wrong_slot',
501
+ ];
502
+
503
+ if (badProposalReasons.includes(reason as BlockProposalValidationFailureReason)) {
504
+ this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
505
+ } else {
506
+ // Node issues so we can't validate
507
+ this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
508
+ }
509
+
510
+ if (
511
+ !escapeHatchOpen &&
512
+ validationResult.reason &&
513
+ SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason)
514
+ ) {
515
+ this.log.info(`Detected invalid block proposal offense`, {
516
+ ...proposalInfo,
517
+ amount: this.config.slashBroadcastedInvalidBlockPenalty,
518
+ offenseType: getOffenseTypeName(OffenseType.BROADCASTED_INVALID_BLOCK_PROPOSAL),
519
+ });
520
+ this.slashInvalidBlock(proposal);
521
+ this.markInvalidProposalSlot(proposal.slotNumber);
522
+ }
523
+ return false;
524
+ }
525
+
526
+ this.log.info(`Validated block proposal for slot ${slotNumber}`, {
527
+ ...proposalInfo,
528
+ inCommittee: partOfCommittee,
529
+ fishermanMode: this.config.fishermanMode || false,
530
+ escapeHatchOpen,
531
+ });
532
+
533
+ if (escapeHatchOpen) {
534
+ this.log.warn(`Escape hatch open for slot ${slotNumber}, rejecting block proposal`, proposalInfo);
535
+ return false;
536
+ }
537
+
538
+ return true;
170
539
  }
171
540
 
172
- async attestToProposal(proposal: BlockProposal): Promise<BlockAttestation | undefined> {
173
- const slotNumber = proposal.slotNumber.toNumber();
174
- const proposalInfo = {
175
- slotNumber,
176
- blockNumber: proposal.payload.header.globalVariables.blockNumber.toNumber(),
177
- archive: proposal.payload.archive.toString(),
178
- txCount: proposal.payload.txHashes.length,
179
- txHashes: proposal.payload.txHashes.map(txHash => txHash.toString()),
180
- };
181
- this.log.verbose(`Received request to attest for slot ${slotNumber}`);
541
+ /**
542
+ * Validate and attest to a checkpoint proposal from a peer.
543
+ * The proposal is received as CheckpointProposalCore (without lastBlock) since
544
+ * the lastBlock is extracted and processed separately via the block handler.
545
+ * @returns Checkpoint attestations if valid, undefined otherwise
546
+ */
547
+ async attestToCheckpointProposal(
548
+ proposal: CheckpointProposalCore,
549
+ _proposalSender: PeerId,
550
+ ): Promise<CheckpointAttestation[] | undefined> {
551
+ const proposalSlotNumber = proposal.slotNumber;
552
+ const proposer = proposal.getSender();
182
553
 
183
- // Check that I am in the committee
184
- if (!(await this.epochCache.isInCommittee(this.keyStore.getAddress()))) {
185
- this.log.verbose(`Not in the committee, skipping attestation`);
554
+ // If escape hatch is open for this slot's epoch, do not attest.
555
+ if (await this.epochCache.isEscapeHatchOpenAtSlot(proposalSlotNumber)) {
556
+ this.log.warn(`Escape hatch open for slot ${proposalSlotNumber}, skipping checkpoint attestation handling`);
186
557
  return undefined;
187
558
  }
188
559
 
189
- // Check that the proposal is from the current proposer, or the next proposer.
190
- const invalidProposal = await this.blockProposalValidator.validate(proposal);
191
- if (invalidProposal) {
192
- this.log.verbose(`Proposal is not valid, skipping attestation`);
560
+ // Early-out for equivocation: refuses if we've already attested to a higher slot.
561
+ if (!this.shouldAttestToSlot(proposalSlotNumber)) {
193
562
  return undefined;
194
563
  }
195
564
 
196
- // Check that all of the transactions in the proposal are available in the tx pool before attesting
197
- this.log.verbose(`Processing attestation for slot ${slotNumber}`, proposalInfo);
198
- try {
199
- await this.ensureTransactionsAreAvailable(proposal);
565
+ // Ignore proposals from ourselves (may happen in HA setups)
566
+ if (proposer && this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
567
+ this.log.debug(`Ignoring block proposal from self for slot ${proposalSlotNumber}`, {
568
+ proposer: proposer.toString(),
569
+ proposalSlotNumber,
570
+ });
571
+ return undefined;
572
+ }
200
573
 
201
- if (this.config.validatorReexecute) {
202
- this.log.verbose(`Re-executing transactions in the proposal before attesting`);
203
- await this.reExecuteTransactions(proposal);
204
- }
205
- } catch (error: any) {
206
- // If the transactions are not available, then we should not attempt to attest
207
- if (error instanceof TransactionsNotAvailableError) {
208
- this.log.error(`Transactions not available, skipping attestation`, error, proposalInfo);
209
- } else {
210
- // This branch most commonly be hit if the transactions are available, but the re-execution fails
211
- // Catch all error handler
212
- this.log.error(`Failed to attest to proposal`, error, proposalInfo);
574
+ // Check that I have any address in the committee where this checkpoint will land before attesting
575
+ const inCommittee = await this.epochCache.filterInCommittee(proposalSlotNumber, this.getValidatorAddresses());
576
+ const partOfCommittee = inCommittee.length > 0;
577
+
578
+ const proposalInfo = {
579
+ proposalSlotNumber,
580
+ archive: proposal.archive.toString(),
581
+ proposer: proposer?.toString(),
582
+ };
583
+ this.log.info(`Received checkpoint proposal for slot ${proposalSlotNumber}`, {
584
+ ...proposalInfo,
585
+ fishermanMode: this.config.fishermanMode || false,
586
+ });
587
+
588
+ // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set).
589
+ // Uses the cached result from the all-nodes callback if available (avoids double validation).
590
+ let checkpointNumber: CheckpointNumber;
591
+ if (this.config.skipCheckpointProposalValidation) {
592
+ this.log.warn(`Skipping checkpoint proposal validation for slot ${proposalSlotNumber}`, proposalInfo);
593
+ checkpointNumber = CheckpointNumber(0);
594
+ } else {
595
+ const validationResult = await this.proposalHandler.handleCheckpointProposal(proposal, proposalInfo);
596
+ if (!validationResult.isValid) {
597
+ this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
598
+ return undefined;
213
599
  }
600
+ checkpointNumber = validationResult.checkpointNumber;
601
+ }
602
+
603
+ // Check that I have any address in current committee before attesting
604
+ // In fisherman mode, we still create attestations for validation even if not in committee
605
+ if (!partOfCommittee && !this.config.fishermanMode) {
606
+ this.log.verbose(`No validator in the current committee, skipping attestation`, proposalInfo);
214
607
  return undefined;
215
608
  }
216
609
 
217
610
  // Provided all of the above checks pass, we can attest to the proposal
218
- this.log.info(`Attesting to proposal for slot ${slotNumber}`, proposalInfo);
611
+ this.log.info(
612
+ `${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${proposalSlotNumber}`,
613
+ {
614
+ ...proposalInfo,
615
+ inCommittee: partOfCommittee,
616
+ fishermanMode: this.config.fishermanMode || false,
617
+ },
618
+ );
619
+
620
+ this.metrics.incSuccessfulAttestations(inCommittee.length);
219
621
 
220
- // If the above function does not throw an error, then we can attest to the proposal
221
- return this.validationService.attestToProposal(proposal);
622
+ // Track epoch participation per attester: count each (attester, epoch) pair at most once
623
+ const proposalEpoch = getEpochAtSlot(proposalSlotNumber, this.epochCache.getL1Constants());
624
+ for (const attester of inCommittee) {
625
+ const key = attester.toString();
626
+ const lastEpoch = this.lastAttestedEpochByAttester.get(key);
627
+ if (lastEpoch === undefined || proposalEpoch > lastEpoch) {
628
+ this.lastAttestedEpochByAttester.set(key, proposalEpoch);
629
+ this.metrics.incAttestedEpochCount(attester);
630
+ }
631
+ }
632
+
633
+ // Determine which validators should attest
634
+ let attestors: EthAddress[];
635
+ if (partOfCommittee) {
636
+ attestors = inCommittee;
637
+ } else if (this.config.fishermanMode) {
638
+ // In fisherman mode, create attestations for validation purposes even if not in committee. These won't be broadcast.
639
+ attestors = this.getValidatorAddresses();
640
+ } else {
641
+ attestors = [];
642
+ }
643
+
644
+ // Only create attestations if we have attestors
645
+ if (attestors.length === 0) {
646
+ return undefined;
647
+ }
648
+
649
+ if (this.config.fishermanMode) {
650
+ // bail out early and don't save attestations to the pool in fisherman mode
651
+ this.log.info(`Creating checkpoint attestations for slot ${proposalSlotNumber}`, {
652
+ ...proposalInfo,
653
+ attestors: attestors.map(a => a.toString()),
654
+ });
655
+ return undefined;
656
+ }
657
+
658
+ return await this.createCheckpointAttestationsFromProposal(proposal, attestors, checkpointNumber);
222
659
  }
223
660
 
224
661
  /**
225
- * Re-execute the transactions in the proposal and check that the state updates match the header state
226
- * @param proposal - The proposal to re-execute
662
+ * Checks if we should attest to a slot based on equivocation prevention rules.
663
+ * @returns true if we should attest, false if we should skip
227
664
  */
228
- async reExecuteTransactions(proposal: BlockProposal) {
229
- const { header, txHashes } = proposal.payload;
665
+ private shouldAttestToSlot(slotNumber: SlotNumber): boolean {
666
+ // If attestToEquivocatedProposals is true, always allow
667
+ if (this.config.attestToEquivocatedProposals) {
668
+ return true;
669
+ }
230
670
 
231
- const txs = (await Promise.all(txHashes.map(tx => this.p2pClient.getTxByHash(tx)))).filter(
232
- tx => tx !== undefined,
233
- ) as Tx[];
671
+ // Check if incoming slot is strictly greater than last attested
672
+ if (this.lastAttestedProposal && slotNumber <= this.lastAttestedProposal.slotNumber) {
673
+ this.log.warn(
674
+ `Refusing to process a proposal for slot ${slotNumber} given we already attested to a proposal for slot ${this.lastAttestedProposal.slotNumber}`,
675
+ );
676
+ return false;
677
+ }
678
+
679
+ return true;
680
+ }
681
+
682
+ private async createCheckpointAttestationsFromProposal(
683
+ proposal: CheckpointProposalCore,
684
+ attestors: EthAddress[] = [],
685
+ checkpointNumber: CheckpointNumber,
686
+ ): Promise<CheckpointAttestation[] | undefined> {
687
+ // Equivocation check: must happen right before signing to minimize the race window
688
+ if (!this.shouldAttestToSlot(proposal.slotNumber)) {
689
+ return undefined;
690
+ }
691
+
692
+ const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors, checkpointNumber);
693
+
694
+ // Track the proposal we attested to (to prevent equivocation)
695
+ this.lastAttestedProposal = proposal;
696
+
697
+ await this.p2pClient.addOwnCheckpointAttestations(attestations);
698
+ return attestations;
699
+ }
700
+
701
+ /**
702
+ * Uploads blobs for a checkpoint to the filestore (fire and forget).
703
+ */
704
+ protected async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
705
+ try {
706
+ const lastBlockHeader = (await this.blockSource.getBlockData({ archive: proposal.archive }))?.header;
707
+ if (!lastBlockHeader) {
708
+ this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
709
+ return;
710
+ }
711
+
712
+ const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
713
+ if (blocks.length === 0) {
714
+ this.log.warn(`No blocks found for blob upload`, proposalInfo);
715
+ return;
716
+ }
234
717
 
235
- // If we cannot request all of the transactions, then we should fail
236
- if (txs.length !== txHashes.length) {
237
- throw new TransactionsNotAvailableError(txHashes);
718
+ const blobFields = blocks.flatMap(b => b.toBlobFields());
719
+ const blobs: Blob[] = await getBlobsPerL1Block(blobFields);
720
+ await this.blobClient.sendBlobsToFilestore(blobs);
721
+ this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
722
+ ...proposalInfo,
723
+ numBlobs: blobs.length,
724
+ });
725
+ } catch (err) {
726
+ this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
238
727
  }
728
+ }
729
+
730
+ private slashInvalidBlock(proposal: BlockProposal) {
731
+ const proposer = proposal.getSender();
239
732
 
240
- // Assertion: This check will fail if re-execution is not enabled
241
- if (this.blockBuilder === undefined) {
242
- throw new BlockBuilderNotProvidedError();
733
+ // Skip if signature is invalid (shouldn't happen since we validate earlier)
734
+ if (!proposer) {
735
+ this.log.warn(`Cannot slash proposal with invalid signature`);
736
+ return;
243
737
  }
244
738
 
245
- // Use the sequencer's block building logic to re-execute the transactions
246
- const stopTimer = this.metrics.reExecutionTimer();
247
- const { block, numFailedTxs } = await this.blockBuilder(txs, header.globalVariables, {
248
- validateOnly: true,
249
- });
250
- stopTimer();
739
+ this.proposersOfInvalidBlocks.add(proposer.toString());
251
740
 
252
- this.log.verbose(`Transaction re-execution complete`);
741
+ this.emit(WANT_TO_SLASH_EVENT, [
742
+ {
743
+ validator: proposer,
744
+ amount: this.config.slashBroadcastedInvalidBlockPenalty,
745
+ offenseType: OffenseType.BROADCASTED_INVALID_BLOCK_PROPOSAL,
746
+ epochOrSlot: BigInt(proposal.slotNumber),
747
+ },
748
+ ]);
749
+ }
750
+
751
+ private handleInvalidCheckpointProposal(
752
+ proposal: CheckpointProposalCore,
753
+ result: CheckpointProposalValidationFailureResult,
754
+ proposalInfo: LogData,
755
+ ): void {
756
+ if (!SLASHABLE_CHECKPOINT_PROPOSAL_VALIDATION_RESULT[result.reason]) {
757
+ return;
758
+ }
253
759
 
254
- if (numFailedTxs > 0) {
255
- await this.metrics.recordFailedReexecution(proposal);
256
- throw new ReExFailedTxsError(numFailedTxs);
760
+ this.markInvalidProposalSlot(proposal.slotNumber);
761
+
762
+ if (this.slashInvalidCheckpointProposal(proposal)) {
763
+ this.log.info(`Detected invalid checkpoint proposal offense`, {
764
+ ...proposalInfo,
765
+ reason: result.reason,
766
+ amount: this.config.slashBroadcastedInvalidCheckpointProposalPenalty,
767
+ offenseType: getOffenseTypeName(OffenseType.BROADCASTED_INVALID_CHECKPOINT_PROPOSAL),
768
+ });
257
769
  }
770
+ }
258
771
 
259
- if (block.body.txEffects.length !== txHashes.length) {
260
- await this.metrics.recordFailedReexecution(proposal);
261
- throw new ReExTimeoutError();
772
+ private slashInvalidCheckpointProposal(proposal: CheckpointProposalCore): boolean {
773
+ const proposer = proposal.getSender();
774
+ if (!proposer) {
775
+ this.log.warn(`Cannot slash checkpoint proposal with invalid signature`, {
776
+ slotNumber: proposal.slotNumber,
777
+ archive: proposal.archive.toString(),
778
+ });
779
+ return false;
262
780
  }
263
781
 
264
- // This function will throw an error if state updates do not match
265
- if (!block.archive.root.equals(proposal.archive)) {
266
- await this.metrics.recordFailedReexecution(proposal);
267
- throw new ReExStateMismatchError();
782
+ const offenseType = OffenseType.BROADCASTED_INVALID_CHECKPOINT_PROPOSAL;
783
+ const offenseKey = `${proposer.toString()}:${offenseType}:${proposal.slotNumber}`;
784
+ if (!this.invalidCheckpointProposalOffenseKeys.addIfAbsent(offenseKey)) {
785
+ return false;
268
786
  }
787
+
788
+ this.emit(WANT_TO_SLASH_EVENT, [
789
+ {
790
+ validator: proposer,
791
+ amount: this.config.slashBroadcastedInvalidCheckpointProposalPenalty,
792
+ offenseType,
793
+ epochOrSlot: BigInt(proposal.slotNumber),
794
+ },
795
+ ]);
796
+ return true;
797
+ }
798
+
799
+ private markInvalidProposalSlot(slotNumber: SlotNumber): void {
800
+ this.slotsWithInvalidProposals.add(slotNumber);
801
+ }
802
+
803
+ private handleCheckpointAttestation(attestation: CheckpointAttestation): void {
804
+ const slotNumber = attestation.slotNumber;
805
+ if (!this.slotsWithInvalidProposals.has(slotNumber) || this.slotsWithProposalEquivocation.has(slotNumber)) {
806
+ return;
807
+ }
808
+
809
+ const attester = attestation.getSender();
810
+ if (!attester) {
811
+ this.log.warn(`Cannot slash checkpoint attestation with invalid signature`, {
812
+ slotNumber,
813
+ archive: attestation.archive.toString(),
814
+ });
815
+ return;
816
+ }
817
+
818
+ this.slashAttestedToInvalidCheckpointProposal(slotNumber, attester);
819
+ }
820
+
821
+ private slashAttestedToInvalidCheckpointProposal(slotNumber: SlotNumber, attester: EthAddress): void {
822
+ const offenseKey = `${attester.toString()}:${OffenseType.ATTESTED_TO_INVALID_CHECKPOINT_PROPOSAL}:${slotNumber}`;
823
+ if (!this.badAttestationOffenseKeys.addIfAbsent(offenseKey)) {
824
+ return;
825
+ }
826
+
827
+ this.log.info(`Detected attestation to invalid checkpoint proposal offense`, {
828
+ attester: attester.toString(),
829
+ slotNumber,
830
+ amount: this.config.slashAttestInvalidCheckpointProposalPenalty,
831
+ offenseType: getOffenseTypeName(OffenseType.ATTESTED_TO_INVALID_CHECKPOINT_PROPOSAL),
832
+ });
833
+
834
+ this.emit(WANT_TO_SLASH_EVENT, [
835
+ {
836
+ validator: attester,
837
+ amount: this.config.slashAttestInvalidCheckpointProposalPenalty,
838
+ offenseType: OffenseType.ATTESTED_TO_INVALID_CHECKPOINT_PROPOSAL,
839
+ epochOrSlot: BigInt(slotNumber),
840
+ },
841
+ ]);
269
842
  }
270
843
 
271
844
  /**
272
- * Ensure that all of the transactions in the proposal are available in the tx pool before attesting
273
- *
274
- * 1. Check if the local tx pool contains all of the transactions in the proposal
275
- * 2. If any transactions are not in the local tx pool, request them from the network
276
- * 3. If we cannot retrieve them from the network, throw an error
277
- * @param proposal - The proposal to attest to
845
+ * Handle detection of a duplicate proposal (equivocation).
846
+ * Emits a slash event when a proposer sends multiple proposals for the same position.
278
847
  */
279
- async ensureTransactionsAreAvailable(proposal: BlockProposal) {
280
- const txHashes: TxHash[] = proposal.payload.txHashes;
281
- const transactionStatuses = await Promise.all(txHashes.map(txHash => this.p2pClient.getTxStatus(txHash)));
848
+ private handleDuplicateProposal(info: DuplicateProposalInfo): void {
849
+ const { slot, proposer, type } = info;
850
+ this.slotsWithProposalEquivocation.add(slot);
282
851
 
283
- const missingTxs = txHashes.filter((_, index) => !['pending', 'mined'].includes(transactionStatuses[index] ?? ''));
852
+ this.log.info(`Detected duplicate ${type} proposal offense from ${proposer.toString()} at slot ${slot}`, {
853
+ proposer: proposer.toString(),
854
+ slot,
855
+ type,
856
+ amount: this.config.slashDuplicateProposalPenalty,
857
+ offenseType: getOffenseTypeName(OffenseType.DUPLICATE_PROPOSAL),
858
+ });
284
859
 
285
- if (missingTxs.length === 0) {
286
- return; // All transactions are available
287
- }
860
+ this.emit(WANT_TO_SLASH_EVENT, [
861
+ {
862
+ validator: proposer,
863
+ amount: this.config.slashDuplicateProposalPenalty,
864
+ offenseType: OffenseType.DUPLICATE_PROPOSAL,
865
+ epochOrSlot: BigInt(slot),
866
+ },
867
+ ]);
868
+
869
+ this.emit(WANT_TO_CLEAR_SLASH_EVENT, [
870
+ {
871
+ offenseType: OffenseType.ATTESTED_TO_INVALID_CHECKPOINT_PROPOSAL,
872
+ epochOrSlot: BigInt(slot),
873
+ },
874
+ ]);
875
+ }
876
+
877
+ /**
878
+ * Handle detection of a duplicate attestation (equivocation).
879
+ * Emits a slash event when an attester signs attestations for different proposals at the same slot.
880
+ */
881
+ private handleDuplicateAttestation(info: DuplicateAttestationInfo): void {
882
+ const { slot, attester } = info;
883
+
884
+ this.log.info(`Detected duplicate attestation offense from ${attester.toString()} at slot ${slot}`, {
885
+ attester: attester.toString(),
886
+ slot,
887
+ amount: this.config.slashDuplicateAttestationPenalty,
888
+ offenseType: getOffenseTypeName(OffenseType.DUPLICATE_ATTESTATION),
889
+ });
890
+
891
+ this.emit(WANT_TO_SLASH_EVENT, [
892
+ {
893
+ validator: attester,
894
+ amount: this.config.slashDuplicateAttestationPenalty,
895
+ offenseType: OffenseType.DUPLICATE_ATTESTATION,
896
+ epochOrSlot: BigInt(slot),
897
+ },
898
+ ]);
899
+ }
288
900
 
289
- this.log.verbose(`Missing ${missingTxs.length} transactions in the tx pool, requesting from the network`);
901
+ async createBlockProposal(
902
+ blockHeader: BlockHeader,
903
+ checkpointNumber: CheckpointNumber,
904
+ indexWithinCheckpoint: IndexWithinCheckpoint,
905
+ inHash: Fr,
906
+ archive: Fr,
907
+ txs: Tx[],
908
+ proposerAddress: EthAddress | undefined,
909
+ options: BlockProposalOptions = {},
910
+ ): Promise<BlockProposal> {
911
+ // Validate that we're not creating a proposal for an older or equal position
912
+ if (this.lastProposedBlock) {
913
+ const lastSlot = this.lastProposedBlock.slotNumber;
914
+ const lastIndex = this.lastProposedBlock.indexWithinCheckpoint;
915
+ const newSlot = blockHeader.globalVariables.slotNumber;
290
916
 
291
- const requestedTxs = await this.p2pClient.requestTxs(missingTxs);
292
- if (requestedTxs.some(tx => tx === undefined)) {
293
- throw new TransactionsNotAvailableError(missingTxs);
917
+ if (newSlot < lastSlot || (newSlot === lastSlot && indexWithinCheckpoint <= lastIndex)) {
918
+ throw new Error(
919
+ `Cannot create block proposal for slot ${newSlot} index ${indexWithinCheckpoint}: ` +
920
+ `already proposed block for slot ${lastSlot} index ${lastIndex}`,
921
+ );
922
+ }
294
923
  }
924
+
925
+ this.log.info(
926
+ `Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`,
927
+ );
928
+ const newProposal = await this.validationService.createBlockProposal(
929
+ blockHeader,
930
+ checkpointNumber,
931
+ indexWithinCheckpoint,
932
+ inHash,
933
+ archive,
934
+ txs,
935
+ proposerAddress,
936
+ {
937
+ ...options,
938
+ broadcastInvalidBlockProposal:
939
+ options.broadcastInvalidBlockProposal || this.config.broadcastInvalidBlockProposal,
940
+ },
941
+ );
942
+ this.lastProposedBlock = newProposal;
943
+ return newProposal;
295
944
  }
296
945
 
297
- async createBlockProposal(header: BlockHeader, archive: Fr, txs: TxHash[]): Promise<BlockProposal | undefined> {
298
- if (this.previousProposal?.slotNumber.equals(header.globalVariables.slotNumber)) {
299
- this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
300
- return Promise.resolve(undefined);
946
+ async createCheckpointProposal(
947
+ checkpointHeader: CheckpointHeader,
948
+ archive: Fr,
949
+ checkpointNumber: CheckpointNumber,
950
+ feeAssetPriceModifier: bigint,
951
+ lastBlockProposal: BlockProposal | undefined,
952
+ proposerAddress: EthAddress | undefined,
953
+ options: CheckpointProposalOptions = {},
954
+ ): Promise<CheckpointProposal> {
955
+ // Validate that we're not creating a proposal for an older or equal slot
956
+ if (this.lastProposedCheckpoint) {
957
+ const lastSlot = this.lastProposedCheckpoint.slotNumber;
958
+ const newSlot = checkpointHeader.slotNumber;
959
+
960
+ if (newSlot <= lastSlot) {
961
+ throw new Error(
962
+ `Cannot create checkpoint proposal for slot ${newSlot}: ` +
963
+ `already proposed checkpoint for slot ${lastSlot}`,
964
+ );
965
+ }
301
966
  }
302
967
 
303
- const newProposal = await this.validationService.createBlockProposal(header, archive, txs);
304
- this.previousProposal = newProposal;
968
+ this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
969
+ const newProposal = await this.validationService.createCheckpointProposal(
970
+ checkpointHeader,
971
+ archive,
972
+ checkpointNumber,
973
+ feeAssetPriceModifier,
974
+ lastBlockProposal,
975
+ proposerAddress,
976
+ options,
977
+ );
978
+ this.lastProposedCheckpoint = newProposal;
979
+ // Self-record this slot's outcome on the re-execution tracker. Proposers don't run their
980
+ // own proposals through `handleCheckpointProposal`, so without this call the proposer's
981
+ // sentinel would see no outcome for slots it proposed and would mis-attribute itself as
982
+ // inactive. We pass the locally-computed `archive` (not `newProposal.archive`, which may
983
+ // be intentionally corrupted under test-only flags); from the proposer's local-view
984
+ // perspective the work it just completed is valid by definition.
985
+ this.proposalHandler.recordOwnCheckpointProposalAsValid(checkpointHeader.slotNumber, archive, checkpointNumber);
305
986
  return newProposal;
306
987
  }
307
988
 
308
- broadcastBlockProposal(proposal: BlockProposal): void {
309
- this.p2pClient.broadcastProposal(proposal);
989
+ async broadcastBlockProposal(proposal: BlockProposal): Promise<void> {
990
+ await this.p2pClient.broadcastProposal(proposal);
991
+ }
992
+
993
+ async signAttestationsAndSigners(
994
+ attestationsAndSigners: CommitteeAttestationsAndSigners,
995
+ proposer: EthAddress,
996
+ slot: SlotNumber,
997
+ checkpointNumber: CheckpointNumber,
998
+ ): Promise<Signature> {
999
+ return await this.validationService.signAttestationsAndSigners(
1000
+ attestationsAndSigners,
1001
+ proposer,
1002
+ slot,
1003
+ checkpointNumber,
1004
+ );
1005
+ }
1006
+
1007
+ async collectOwnAttestations(
1008
+ proposal: CheckpointProposal,
1009
+ checkpointNumber: CheckpointNumber,
1010
+ ): Promise<CheckpointAttestation[]> {
1011
+ const slot = proposal.slotNumber;
1012
+ const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
1013
+ this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, { inCommittee });
1014
+ const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee, checkpointNumber);
1015
+
1016
+ if (!attestations) {
1017
+ return [];
1018
+ }
1019
+
1020
+ // We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
1021
+ // other nodes can see that our validators did attest to this block proposal, and do not slash us
1022
+ // due to inactivity for missed attestations.
1023
+ void this.p2pClient.broadcastCheckpointAttestations(attestations).catch(err => {
1024
+ this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
1025
+ });
1026
+ return attestations;
310
1027
  }
311
1028
 
312
- // TODO(https://github.com/AztecProtocol/aztec-packages/issues/7962)
313
- async collectAttestations(proposal: BlockProposal, required: number, deadline: Date): Promise<BlockAttestation[]> {
314
- // Wait and poll the p2pClient's attestation pool for this block until we have enough attestations
315
- const slot = proposal.payload.header.globalVariables.slotNumber.toBigInt();
1029
+ async collectAttestations(
1030
+ proposal: CheckpointProposal,
1031
+ required: number,
1032
+ deadline: Date,
1033
+ checkpointNumber: CheckpointNumber,
1034
+ ): Promise<CheckpointAttestation[]> {
1035
+ // Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
1036
+ const slot = proposal.slotNumber;
316
1037
  this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
317
1038
 
318
1039
  if (+deadline < this.dateProvider.now()) {
319
1040
  this.log.error(
320
1041
  `Deadline ${deadline.toISOString()} for collecting ${required} attestations for slot ${slot} is in the past`,
321
1042
  );
322
- throw new AttestationTimeoutError(required, slot);
1043
+ throw new AttestationTimeoutError(0, required, slot);
323
1044
  }
324
1045
 
325
- const proposalId = proposal.archive.toString();
326
- const myAttestation = await this.validationService.attestToProposal(proposal);
1046
+ await this.collectOwnAttestations(proposal, checkpointNumber);
327
1047
 
328
- let attestations: BlockAttestation[] = [];
1048
+ const proposalPayloadHash = proposal.getPayloadHash();
1049
+ const myAddresses = this.getValidatorAddresses();
1050
+
1051
+ let attestations: CheckpointAttestation[] = [];
329
1052
  while (true) {
330
- const collectedAttestations = [myAttestation, ...(await this.p2pClient.getAttestationsForSlot(slot, proposalId))];
331
- const oldSenders = await Promise.all(attestations.map(attestation => attestation.getSender()));
1053
+ // The pool already filters by proposal payload hash; if any attestation slips through with a
1054
+ // mismatched payload hash, drop it defensively. Equivocations are emitted as separate slash
1055
+ // events from libp2p_service.
1056
+ const collectedAttestations = await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalPayloadHash);
1057
+
1058
+ // Log new attestations we collected
1059
+ const oldSenders = attestations.map(attestation => attestation.getSender());
332
1060
  for (const collected of collectedAttestations) {
333
- const collectedSender = await collected.getSender();
334
- if (!oldSenders.some(sender => sender.equals(collectedSender))) {
1061
+ const collectedSender = collected.getSender();
1062
+ // Skip attestations with invalid signatures. Should not happen as we don't add invalid attestations to our pool.
1063
+ if (!collectedSender) {
1064
+ this.log.warn(`Skipping attestation with invalid signature for slot ${slot}`);
1065
+ continue;
1066
+ }
1067
+ if (
1068
+ !myAddresses.some(address => address.equals(collectedSender)) &&
1069
+ !oldSenders.some(sender => sender?.equals(collectedSender))
1070
+ ) {
335
1071
  this.log.debug(`Received attestation for slot ${slot} from ${collectedSender.toString()}`);
336
1072
  }
337
1073
  }
@@ -344,19 +1080,36 @@ export class ValidatorClient extends WithTracer implements Validator {
344
1080
 
345
1081
  if (+deadline < this.dateProvider.now()) {
346
1082
  this.log.error(`Timeout ${deadline.toISOString()} waiting for ${required} attestations for slot ${slot}`);
347
- throw new AttestationTimeoutError(required, slot);
1083
+ throw new AttestationTimeoutError(attestations.length, required, slot);
348
1084
  }
349
1085
 
350
- this.log.debug(`Collected ${attestations.length} attestations so far`);
1086
+ this.log.debug(`Collected ${attestations.length} of ${required} attestations so far`);
351
1087
  await sleep(this.config.attestationPollingIntervalMs);
352
1088
  }
353
1089
  }
354
- }
355
1090
 
356
- function validatePrivateKey(privateKey: string): Buffer32 {
357
- try {
358
- return Buffer32.fromString(privateKey);
359
- } catch (error) {
360
- throw new InvalidValidatorPrivateKeyError();
1091
+ private async handleAuthRequest(peer: PeerId, msg: Buffer): Promise<Buffer> {
1092
+ const authRequest = AuthRequest.fromBuffer(msg);
1093
+ const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch(_ => undefined);
1094
+ if (statusMessage === undefined) {
1095
+ return Buffer.alloc(0);
1096
+ }
1097
+
1098
+ // Find a validator address that is in the set
1099
+ const allRegisteredValidators = await this.epochCache.getRegisteredValidators();
1100
+ const addressToUse = this.getValidatorAddresses().find(
1101
+ address => allRegisteredValidators.find(v => v.equals(address)) !== undefined,
1102
+ );
1103
+ if (addressToUse === undefined) {
1104
+ // We don't have a registered address
1105
+ return Buffer.alloc(0);
1106
+ }
1107
+
1108
+ const payloadToSign = authRequest.getPayloadToSign();
1109
+ // AUTH_REQUEST doesn't require HA protection - multiple signatures are safe
1110
+ const context: SigningContext = { dutyType: DutyType.AUTH_REQUEST };
1111
+ const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign, context);
1112
+ const authResponse = new AuthResponse(statusMessage, signature);
1113
+ return authResponse.toBuffer();
361
1114
  }
362
1115
  }