@aztec/validator-client 0.0.0-test.1 → 0.0.1-commit.0b941701

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