@aztec/validator-client 0.0.0-test.0 → 0.0.1-commit.001888fc

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