@aztec/validator-client 0.0.0-test.1 → 0.0.1-fake-ceab37513c

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 (57) hide show
  1. package/dest/block_proposal_handler.d.ts +52 -0
  2. package/dest/block_proposal_handler.d.ts.map +1 -0
  3. package/dest/block_proposal_handler.js +286 -0
  4. package/dest/config.d.ts +2 -13
  5. package/dest/config.d.ts.map +1 -1
  6. package/dest/config.js +31 -7
  7. package/dest/duties/validation_service.d.ts +11 -6
  8. package/dest/duties/validation_service.d.ts.map +1 -1
  9. package/dest/duties/validation_service.js +27 -10
  10. package/dest/factory.d.ts +21 -4
  11. package/dest/factory.d.ts.map +1 -1
  12. package/dest/factory.js +13 -6
  13. package/dest/index.d.ts +3 -1
  14. package/dest/index.d.ts.map +1 -1
  15. package/dest/index.js +3 -1
  16. package/dest/key_store/index.d.ts +2 -0
  17. package/dest/key_store/index.d.ts.map +1 -1
  18. package/dest/key_store/index.js +2 -0
  19. package/dest/key_store/interface.d.ts +54 -5
  20. package/dest/key_store/interface.d.ts.map +1 -1
  21. package/dest/key_store/interface.js +3 -3
  22. package/dest/key_store/local_key_store.d.ts +40 -10
  23. package/dest/key_store/local_key_store.d.ts.map +1 -1
  24. package/dest/key_store/local_key_store.js +63 -16
  25. package/dest/key_store/node_keystore_adapter.d.ts +138 -0
  26. package/dest/key_store/node_keystore_adapter.d.ts.map +1 -0
  27. package/dest/key_store/node_keystore_adapter.js +316 -0
  28. package/dest/key_store/web3signer_key_store.d.ts +67 -0
  29. package/dest/key_store/web3signer_key_store.d.ts.map +1 -0
  30. package/dest/key_store/web3signer_key_store.js +153 -0
  31. package/dest/metrics.d.ts +9 -4
  32. package/dest/metrics.d.ts.map +1 -1
  33. package/dest/metrics.js +41 -15
  34. package/dest/validator.d.ts +46 -61
  35. package/dest/validator.d.ts.map +1 -1
  36. package/dest/validator.js +242 -164
  37. package/package.json +25 -19
  38. package/src/block_proposal_handler.ts +343 -0
  39. package/src/config.ts +42 -22
  40. package/src/duties/validation_service.ts +57 -12
  41. package/src/factory.ts +56 -11
  42. package/src/index.ts +3 -1
  43. package/src/key_store/index.ts +2 -0
  44. package/src/key_store/interface.ts +61 -5
  45. package/src/key_store/local_key_store.ts +67 -17
  46. package/src/key_store/node_keystore_adapter.ts +375 -0
  47. package/src/key_store/web3signer_key_store.ts +195 -0
  48. package/src/metrics.ts +49 -17
  49. package/src/validator.ts +359 -232
  50. package/dest/errors/index.d.ts +0 -2
  51. package/dest/errors/index.d.ts.map +0 -1
  52. package/dest/errors/index.js +0 -1
  53. package/dest/errors/validator.error.d.ts +0 -29
  54. package/dest/errors/validator.error.d.ts.map +0 -1
  55. package/dest/errors/validator.error.js +0 -45
  56. package/src/errors/index.ts +0 -1
  57. package/src/errors/validator.error.ts +0 -55
package/src/validator.ts CHANGED
@@ -1,337 +1,440 @@
1
1
  import type { EpochCache } from '@aztec/epoch-cache';
2
- import { Buffer32 } from '@aztec/foundation/buffer';
3
- import type { Fr } from '@aztec/foundation/fields';
2
+ import type { EthAddress } from '@aztec/foundation/eth-address';
3
+ import type { Signature } from '@aztec/foundation/eth-signature';
4
+ import { Fr } from '@aztec/foundation/fields';
4
5
  import { createLogger } from '@aztec/foundation/log';
5
6
  import { RunningPromise } from '@aztec/foundation/running-promise';
6
7
  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';
8
+ import { DateProvider } from '@aztec/foundation/timer';
9
+ import type { KeystoreManager } from '@aztec/node-keystore';
10
+ import type { P2P, PeerId, TxProvider } from '@aztec/p2p';
11
+ import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
12
+ import { OffenseType, WANT_TO_SLASH_EVENT, type Watcher, type WatcherEmitter } from '@aztec/slasher';
13
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
14
+ import type { CommitteeAttestationsAndSigners, L2BlockSource } from '@aztec/stdlib/block';
15
+ import type { IFullNodeBlockBuilder, Validator, ValidatorClientFullConfig } from '@aztec/stdlib/interfaces/server';
16
+ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
17
+ import type { BlockAttestation, BlockProposal, BlockProposalOptions } from '@aztec/stdlib/p2p';
18
+ import type { ProposedBlockHeader, StateReference, Tx } from '@aztec/stdlib/tx';
19
+ import { AttestationTimeoutError } from '@aztec/stdlib/validators';
20
+ import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
21
+
22
+ import { EventEmitter } from 'events';
23
+ import type { TypedDataDefinition } from 'viem';
24
+
25
+ import { BlockProposalHandler, type BlockProposalValidationFailureReason } from './block_proposal_handler.js';
16
26
  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';
27
+ import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
28
28
  import { ValidatorMetrics } from './metrics.js';
29
29
 
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
- }
30
+ // We maintain a set of proposers who have proposed invalid blocks.
31
+ // Just cap the set to avoid unbounded growth.
32
+ const MAX_PROPOSERS_OF_INVALID_BLOCKS = 1000;
33
+
34
+ // What errors from the block proposal handler result in slashing
35
+ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT: BlockProposalValidationFailureReason[] = [
36
+ 'state_mismatch',
37
+ 'failed_txs',
38
+ ];
59
39
 
60
40
  /**
61
41
  * Validator Client
62
42
  */
63
- export class ValidatorClient extends WithTracer implements Validator {
43
+ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter) implements Validator, Watcher {
44
+ public readonly tracer: Tracer;
64
45
  private validationService: ValidationService;
65
46
  private metrics: ValidatorMetrics;
66
47
 
48
+ // Whether it has already registered handlers on the p2p client
49
+ private hasRegisteredHandlers = false;
50
+
67
51
  // Used to check if we are sending the same proposal twice
68
52
  private previousProposal?: BlockProposal;
69
53
 
70
- // Callback registered to: sequencer.buildBlock
71
- private blockBuilder?: BlockBuilderCallback = undefined;
72
-
54
+ private lastEpochForCommitteeUpdateLoop: bigint | undefined;
73
55
  private epochCacheUpdateLoop: RunningPromise;
74
56
 
75
- private blockProposalValidator: BlockProposalValidator;
57
+ private proposersOfInvalidBlocks: Set<string> = new Set();
76
58
 
77
- constructor(
78
- private keyStore: ValidatorKeyStore,
59
+ protected constructor(
60
+ private keyStore: NodeKeystoreAdapter,
79
61
  private epochCache: EpochCache,
80
62
  private p2pClient: P2P,
81
- private config: ValidatorClientConfig,
63
+ private blockProposalHandler: BlockProposalHandler,
64
+ private config: ValidatorClientFullConfig,
82
65
  private dateProvider: DateProvider = new DateProvider(),
83
66
  telemetry: TelemetryClient = getTelemetryClient(),
84
67
  private log = createLogger('validator'),
85
68
  ) {
86
- // Instantiate tracer
87
- super(telemetry, 'Validator');
69
+ super();
70
+ this.tracer = telemetry.getTracer('Validator');
88
71
  this.metrics = new ValidatorMetrics(telemetry);
89
72
 
90
73
  this.validationService = new ValidationService(keyStore);
91
74
 
92
- this.blockProposalValidator = new BlockProposalValidator(epochCache);
93
-
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
- );
75
+ // Refresh epoch cache every second to trigger alert if participation in committee changes
76
+ this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), log, 1000);
77
+
78
+ const myAddresses = this.getValidatorAddresses();
79
+ this.log.verbose(`Initialized validator with addresses: ${myAddresses.map(a => a.toString()).join(', ')}`);
80
+ }
104
81
 
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}`);
82
+ public static validateKeyStoreConfiguration(keyStoreManager: KeystoreManager) {
83
+ const validatorKeyStore = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
84
+ const validatorAddresses = validatorKeyStore.getAddresses();
85
+ // Verify that we can retrieve all required data from the key store
86
+ for (const address of validatorAddresses) {
87
+ // Functions throw if required data is not available
88
+ try {
89
+ validatorKeyStore.getCoinbaseAddress(address);
90
+ validatorKeyStore.getFeeRecipient(address);
91
+ } catch (error) {
92
+ throw new Error(`Failed to retrieve required data for validator address ${address}, error: ${error}`);
112
93
  }
113
- });
114
94
 
115
- this.log.verbose(`Initialized validator with address ${this.keyStore.getAddress().toString()}`);
95
+ const publisherAddresses = validatorKeyStore.getPublisherAddresses(address);
96
+ if (!publisherAddresses.length) {
97
+ throw new Error(`No publisher addresses found for validator address ${address}`);
98
+ }
99
+ }
100
+ }
101
+
102
+ private async handleEpochCommitteeUpdate() {
103
+ try {
104
+ const { committee, epoch } = await this.epochCache.getCommittee('next');
105
+ if (!committee) {
106
+ this.log.trace(`No committee found for slot`);
107
+ return;
108
+ }
109
+ if (epoch !== this.lastEpochForCommitteeUpdateLoop) {
110
+ const me = this.getValidatorAddresses();
111
+ const committeeSet = new Set(committee.map(v => v.toString()));
112
+ const inCommittee = me.filter(a => committeeSet.has(a.toString()));
113
+ if (inCommittee.length > 0) {
114
+ this.log.info(
115
+ `Validators ${inCommittee.map(a => a.toString()).join(',')} are on the validator committee for epoch ${epoch}`,
116
+ );
117
+ } else {
118
+ this.log.verbose(
119
+ `Validators ${me.map(a => a.toString()).join(', ')} are not on the validator committee for epoch ${epoch}`,
120
+ );
121
+ }
122
+ this.lastEpochForCommitteeUpdateLoop = epoch;
123
+ }
124
+ } catch (err) {
125
+ this.log.error(`Error updating epoch committee`, err);
126
+ }
116
127
  }
117
128
 
118
129
  static new(
119
- config: ValidatorClientConfig,
130
+ config: ValidatorClientFullConfig,
131
+ blockBuilder: IFullNodeBlockBuilder,
120
132
  epochCache: EpochCache,
121
133
  p2pClient: P2P,
134
+ blockSource: L2BlockSource,
135
+ l1ToL2MessageSource: L1ToL2MessageSource,
136
+ txProvider: TxProvider,
137
+ keyStoreManager: KeystoreManager,
122
138
  dateProvider: DateProvider = new DateProvider(),
123
139
  telemetry: TelemetryClient = getTelemetryClient(),
124
140
  ) {
125
- if (!config.validatorPrivateKey) {
126
- throw new InvalidValidatorPrivateKeyError();
127
- }
141
+ const metrics = new ValidatorMetrics(telemetry);
142
+ const blockProposalValidator = new BlockProposalValidator(epochCache, {
143
+ txsPermitted: !config.disableTransactions,
144
+ });
145
+ const blockProposalHandler = new BlockProposalHandler(
146
+ blockBuilder,
147
+ blockSource,
148
+ l1ToL2MessageSource,
149
+ txProvider,
150
+ blockProposalValidator,
151
+ config,
152
+ metrics,
153
+ dateProvider,
154
+ telemetry,
155
+ );
128
156
 
129
- const privateKey = validatePrivateKey(config.validatorPrivateKey);
130
- const localKeyStore = new LocalKeyStore(privateKey);
157
+ const validator = new ValidatorClient(
158
+ NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager),
159
+ epochCache,
160
+ p2pClient,
161
+ blockProposalHandler,
162
+ config,
163
+ dateProvider,
164
+ telemetry,
165
+ );
131
166
 
132
- const validator = new ValidatorClient(localKeyStore, epochCache, p2pClient, config, dateProvider, telemetry);
133
- validator.registerBlockProposalHandler();
134
167
  return validator;
135
168
  }
136
169
 
137
- public async start() {
138
- // Sync the committee from the smart contract
139
- // https://github.com/AztecProtocol/aztec-packages/issues/7962
170
+ public getValidatorAddresses() {
171
+ return this.keyStore
172
+ .getAddresses()
173
+ .filter(addr => !this.config.disabledValidators.some(disabled => disabled.equals(addr)));
174
+ }
140
175
 
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()}`);
147
- }
148
- this.epochCacheUpdateLoop.start();
149
- return Promise.resolve();
176
+ public getBlockProposalHandler() {
177
+ return this.blockProposalHandler;
150
178
  }
151
179
 
152
- public async stop() {
153
- await this.epochCacheUpdateLoop.stop();
180
+ // Proxy method for backwards compatibility with tests
181
+ public reExecuteTransactions(
182
+ proposal: BlockProposal,
183
+ blockNumber: number,
184
+ txs: any[],
185
+ l1ToL2Messages: Fr[],
186
+ ): Promise<any> {
187
+ return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
154
188
  }
155
189
 
156
- public registerBlockProposalHandler() {
157
- const handler = (block: BlockProposal): Promise<BlockAttestation | undefined> => {
158
- return this.attestToProposal(block);
159
- };
160
- this.p2pClient.registerBlockProposalHandler(handler);
190
+ public signWithAddress(addr: EthAddress, msg: TypedDataDefinition) {
191
+ return this.keyStore.signTypedDataWithAddress(addr, msg);
161
192
  }
162
193
 
163
- /**
164
- * Register a callback function for building a block
165
- *
166
- * We reuse the sequencer's block building functionality for re-execution
167
- */
168
- public registerBlockBuilder(blockBuilder: BlockBuilderCallback) {
169
- this.blockBuilder = blockBuilder;
194
+ public getCoinbaseForAttestor(attestor: EthAddress): EthAddress {
195
+ return this.keyStore.getCoinbaseAddress(attestor);
170
196
  }
171
197
 
172
- async attestToProposal(proposal: BlockProposal): Promise<BlockAttestation | undefined> {
173
- const slotNumber = proposal.slotNumber.toNumber();
174
- const proposalInfo = {
175
- slotNumber,
176
- blockNumber: proposal.payload.header.globalVariables.blockNumber.toNumber(),
177
- archive: proposal.payload.archive.toString(),
178
- txCount: proposal.payload.txHashes.length,
179
- txHashes: proposal.payload.txHashes.map(txHash => txHash.toString()),
180
- };
181
- this.log.verbose(`Received request to attest for slot ${slotNumber}`);
182
-
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;
187
- }
198
+ public getFeeRecipientForAttestor(attestor: EthAddress): AztecAddress {
199
+ return this.keyStore.getFeeRecipient(attestor);
200
+ }
188
201
 
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;
202
+ public getConfig(): ValidatorClientFullConfig {
203
+ return this.config;
204
+ }
205
+
206
+ public updateConfig(config: Partial<ValidatorClientFullConfig>) {
207
+ this.config = { ...this.config, ...config };
208
+ }
209
+
210
+ public async start() {
211
+ if (this.epochCacheUpdateLoop.isRunning()) {
212
+ this.log.warn(`Validator client already started`);
213
+ return;
194
214
  }
195
215
 
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);
216
+ await this.registerHandlers();
200
217
 
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
- }
214
- return undefined;
218
+ const myAddresses = this.getValidatorAddresses();
219
+ const inCommittee = await this.epochCache.filterInCommittee('now', myAddresses);
220
+ if (inCommittee.length > 0) {
221
+ this.log.info(
222
+ `Started validator with addresses in current validator committee: ${inCommittee
223
+ .map(a => a.toString())
224
+ .join(', ')}`,
225
+ );
226
+ } else {
227
+ this.log.info(`Started validator with addresses: ${myAddresses.map(a => a.toString()).join(', ')}`);
215
228
  }
229
+ this.epochCacheUpdateLoop.start();
216
230
 
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);
231
+ return Promise.resolve();
232
+ }
219
233
 
220
- // If the above function does not throw an error, then we can attest to the proposal
221
- return this.validationService.attestToProposal(proposal);
234
+ public async stop() {
235
+ await this.epochCacheUpdateLoop.stop();
222
236
  }
223
237
 
224
- /**
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
227
- */
228
- async reExecuteTransactions(proposal: BlockProposal) {
229
- const { header, txHashes } = proposal.payload;
238
+ /** Register handlers on the p2p client */
239
+ public async registerHandlers() {
240
+ if (!this.hasRegisteredHandlers) {
241
+ this.hasRegisteredHandlers = true;
242
+ this.log.debug(`Registering validator handlers for p2p client`);
230
243
 
231
- const txs = (await Promise.all(txHashes.map(tx => this.p2pClient.getTxByHash(tx)))).filter(
232
- tx => tx !== undefined,
233
- ) as Tx[];
244
+ const handler = (block: BlockProposal, proposalSender: PeerId): Promise<BlockAttestation[] | undefined> =>
245
+ this.attestToProposal(block, proposalSender);
246
+ this.p2pClient.registerBlockProposalHandler(handler);
234
247
 
235
- // If we cannot request all of the transactions, then we should fail
236
- if (txs.length !== txHashes.length) {
237
- throw new TransactionsNotAvailableError(txHashes);
248
+ const myAddresses = this.getValidatorAddresses();
249
+ this.p2pClient.registerThisValidatorAddresses(myAddresses);
250
+
251
+ await this.p2pClient.addReqRespSubProtocol(ReqRespSubProtocol.AUTH, this.handleAuthRequest.bind(this));
238
252
  }
253
+ }
239
254
 
240
- // Assertion: This check will fail if re-execution is not enabled
241
- if (this.blockBuilder === undefined) {
242
- throw new BlockBuilderNotProvidedError();
255
+ async attestToProposal(proposal: BlockProposal, proposalSender: PeerId): Promise<BlockAttestation[] | undefined> {
256
+ const slotNumber = proposal.slotNumber.toBigInt();
257
+ const proposer = proposal.getSender();
258
+
259
+ // Reject proposals with invalid signatures
260
+ if (!proposer) {
261
+ this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
262
+ return undefined;
243
263
  }
244
264
 
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,
265
+ // Check that I have any address in current committee before attesting
266
+ const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
267
+ const partOfCommittee = inCommittee.length > 0;
268
+ const incFailedAttestation = (reason: string) => this.metrics.incFailedAttestations(1, reason, partOfCommittee);
269
+
270
+ const proposalInfo = { ...proposal.toBlockInfo(), proposer: proposer.toString() };
271
+ this.log.info(`Received proposal for slot ${slotNumber}`, {
272
+ ...proposalInfo,
273
+ txHashes: proposal.txHashes.map(t => t.toString()),
249
274
  });
250
- stopTimer();
251
275
 
252
- this.log.verbose(`Transaction re-execution complete`);
276
+ // Reexecute txs if we are part of the committee so we can attest, or if slashing is enabled so we can slash
277
+ // invalid proposals even when not in the committee, or if we are configured to always reexecute for monitoring purposes.
278
+ const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals } = this.config;
279
+ const shouldReexecute =
280
+ (slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute) ||
281
+ (partOfCommittee && validatorReexecute) ||
282
+ alwaysReexecuteBlockProposals;
283
+
284
+ const validationResult = await this.blockProposalHandler.handleBlockProposal(
285
+ proposal,
286
+ proposalSender,
287
+ !!shouldReexecute,
288
+ );
253
289
 
254
- if (numFailedTxs > 0) {
255
- await this.metrics.recordFailedReexecution(proposal);
256
- throw new ReExFailedTxsError(numFailedTxs);
290
+ if (!validationResult.isValid) {
291
+ this.log.warn(`Proposal validation failed: ${validationResult.reason}`, proposalInfo);
292
+ incFailedAttestation(validationResult.reason || 'unknown');
293
+
294
+ // Slash invalid block proposals
295
+ if (
296
+ validationResult.reason &&
297
+ SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) &&
298
+ slashBroadcastedInvalidBlockPenalty > 0n
299
+ ) {
300
+ this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
301
+ this.slashInvalidBlock(proposal);
302
+ }
303
+ return undefined;
257
304
  }
258
305
 
259
- if (block.body.txEffects.length !== txHashes.length) {
260
- await this.metrics.recordFailedReexecution(proposal);
261
- throw new ReExTimeoutError();
306
+ // Check that I have any address in current committee before attesting
307
+ if (!partOfCommittee) {
308
+ this.log.verbose(`No validator in the current committee, skipping attestation`, proposalInfo);
309
+ return undefined;
262
310
  }
263
311
 
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();
268
- }
312
+ // Provided all of the above checks pass, we can attest to the proposal
313
+ this.log.info(`Attesting to proposal for block at slot ${slotNumber}`, proposalInfo);
314
+ this.metrics.incAttestations(inCommittee.length);
315
+
316
+ // If the above function does not throw an error, then we can attest to the proposal
317
+ return this.createBlockAttestationsFromProposal(proposal, inCommittee);
269
318
  }
270
319
 
271
- /**
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
278
- */
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)));
282
-
283
- const missingTxs = txHashes.filter((_, index) => !['pending', 'mined'].includes(transactionStatuses[index] ?? ''));
284
-
285
- if (missingTxs.length === 0) {
286
- return; // All transactions are available
287
- }
320
+ private slashInvalidBlock(proposal: BlockProposal) {
321
+ const proposer = proposal.getSender();
288
322
 
289
- this.log.verbose(`Missing ${missingTxs.length} transactions in the tx pool, requesting from the network`);
323
+ // Skip if signature is invalid (shouldn't happen since we validate earlier)
324
+ if (!proposer) {
325
+ this.log.warn(`Cannot slash proposal with invalid signature`);
326
+ return;
327
+ }
290
328
 
291
- const requestedTxs = await this.p2pClient.requestTxs(missingTxs);
292
- if (requestedTxs.some(tx => tx === undefined)) {
293
- throw new TransactionsNotAvailableError(missingTxs);
329
+ // Trim the set if it's too big.
330
+ if (this.proposersOfInvalidBlocks.size > MAX_PROPOSERS_OF_INVALID_BLOCKS) {
331
+ // remove oldest proposer. `values` is guaranteed to be in insertion order.
332
+ this.proposersOfInvalidBlocks.delete(this.proposersOfInvalidBlocks.values().next().value!);
294
333
  }
334
+
335
+ this.proposersOfInvalidBlocks.add(proposer.toString());
336
+
337
+ this.emit(WANT_TO_SLASH_EVENT, [
338
+ {
339
+ validator: proposer,
340
+ amount: this.config.slashBroadcastedInvalidBlockPenalty,
341
+ offenseType: OffenseType.BROADCASTED_INVALID_BLOCK_PROPOSAL,
342
+ epochOrSlot: proposal.slotNumber.toBigInt(),
343
+ },
344
+ ]);
295
345
  }
296
346
 
297
- async createBlockProposal(header: BlockHeader, archive: Fr, txs: TxHash[]): Promise<BlockProposal | undefined> {
298
- if (this.previousProposal?.slotNumber.equals(header.globalVariables.slotNumber)) {
347
+ async createBlockProposal(
348
+ blockNumber: number,
349
+ header: ProposedBlockHeader,
350
+ archive: Fr,
351
+ stateReference: StateReference,
352
+ txs: Tx[],
353
+ proposerAddress: EthAddress | undefined,
354
+ options: BlockProposalOptions,
355
+ ): Promise<BlockProposal | undefined> {
356
+ if (this.previousProposal?.slotNumber.equals(header.slotNumber)) {
299
357
  this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
300
358
  return Promise.resolve(undefined);
301
359
  }
302
360
 
303
- const newProposal = await this.validationService.createBlockProposal(header, archive, txs);
361
+ const newProposal = await this.validationService.createBlockProposal(
362
+ header,
363
+ archive,
364
+ stateReference,
365
+ txs,
366
+ proposerAddress,
367
+ options,
368
+ );
304
369
  this.previousProposal = newProposal;
305
370
  return newProposal;
306
371
  }
307
372
 
308
- broadcastBlockProposal(proposal: BlockProposal): void {
309
- this.p2pClient.broadcastProposal(proposal);
373
+ async broadcastBlockProposal(proposal: BlockProposal): Promise<void> {
374
+ await this.p2pClient.broadcastProposal(proposal);
375
+ }
376
+
377
+ async signAttestationsAndSigners(
378
+ attestationsAndSigners: CommitteeAttestationsAndSigners,
379
+ proposer: EthAddress | undefined,
380
+ ): Promise<Signature> {
381
+ return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
382
+ }
383
+
384
+ async collectOwnAttestations(proposal: BlockProposal): Promise<BlockAttestation[]> {
385
+ const slot = proposal.payload.header.slotNumber.toBigInt();
386
+ const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
387
+ this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, { inCommittee });
388
+ return this.createBlockAttestationsFromProposal(proposal, inCommittee);
310
389
  }
311
390
 
312
- // TODO(https://github.com/AztecProtocol/aztec-packages/issues/7962)
313
391
  async collectAttestations(proposal: BlockProposal, required: number, deadline: Date): Promise<BlockAttestation[]> {
314
392
  // 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();
393
+ const slot = proposal.payload.header.slotNumber.toBigInt();
316
394
  this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
317
395
 
318
396
  if (+deadline < this.dateProvider.now()) {
319
397
  this.log.error(
320
398
  `Deadline ${deadline.toISOString()} for collecting ${required} attestations for slot ${slot} is in the past`,
321
399
  );
322
- throw new AttestationTimeoutError(required, slot);
400
+ throw new AttestationTimeoutError(0, required, slot);
323
401
  }
324
402
 
403
+ await this.collectOwnAttestations(proposal);
404
+
325
405
  const proposalId = proposal.archive.toString();
326
- const myAttestation = await this.validationService.attestToProposal(proposal);
406
+ const myAddresses = this.getValidatorAddresses();
327
407
 
328
408
  let attestations: BlockAttestation[] = [];
329
409
  while (true) {
330
- const collectedAttestations = [myAttestation, ...(await this.p2pClient.getAttestationsForSlot(slot, proposalId))];
331
- const oldSenders = await Promise.all(attestations.map(attestation => attestation.getSender()));
410
+ // Filter out attestations with a mismatching payload. This should NOT happen since we have verified
411
+ // the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
412
+ const collectedAttestations = (await this.p2pClient.getAttestationsForSlot(slot, proposalId)).filter(
413
+ attestation => {
414
+ if (!attestation.payload.equals(proposal.payload)) {
415
+ this.log.warn(
416
+ `Received attestation for slot ${slot} with mismatched payload from ${attestation.getSender()?.toString()}`,
417
+ { attestationPayload: attestation.payload, proposalPayload: proposal.payload },
418
+ );
419
+ return false;
420
+ }
421
+ return true;
422
+ },
423
+ );
424
+
425
+ // Log new attestations we collected
426
+ const oldSenders = attestations.map(attestation => attestation.getSender());
332
427
  for (const collected of collectedAttestations) {
333
- const collectedSender = await collected.getSender();
334
- if (!oldSenders.some(sender => sender.equals(collectedSender))) {
428
+ const collectedSender = collected.getSender();
429
+ // Skip attestations with invalid signatures
430
+ if (!collectedSender) {
431
+ this.log.warn(`Skipping attestation with invalid signature for slot ${slot}`);
432
+ continue;
433
+ }
434
+ if (
435
+ !myAddresses.some(address => address.equals(collectedSender)) &&
436
+ !oldSenders.some(sender => sender?.equals(collectedSender))
437
+ ) {
335
438
  this.log.debug(`Received attestation for slot ${slot} from ${collectedSender.toString()}`);
336
439
  }
337
440
  }
@@ -344,19 +447,43 @@ export class ValidatorClient extends WithTracer implements Validator {
344
447
 
345
448
  if (+deadline < this.dateProvider.now()) {
346
449
  this.log.error(`Timeout ${deadline.toISOString()} waiting for ${required} attestations for slot ${slot}`);
347
- throw new AttestationTimeoutError(required, slot);
450
+ throw new AttestationTimeoutError(attestations.length, required, slot);
348
451
  }
349
452
 
350
453
  this.log.debug(`Collected ${attestations.length} attestations so far`);
351
454
  await sleep(this.config.attestationPollingIntervalMs);
352
455
  }
353
456
  }
354
- }
355
457
 
356
- function validatePrivateKey(privateKey: string): Buffer32 {
357
- try {
358
- return Buffer32.fromString(privateKey);
359
- } catch (error) {
360
- throw new InvalidValidatorPrivateKeyError();
458
+ private async createBlockAttestationsFromProposal(
459
+ proposal: BlockProposal,
460
+ attestors: EthAddress[] = [],
461
+ ): Promise<BlockAttestation[]> {
462
+ const attestations = await this.validationService.attestToProposal(proposal, attestors);
463
+ await this.p2pClient.addAttestations(attestations);
464
+ return attestations;
465
+ }
466
+
467
+ private async handleAuthRequest(peer: PeerId, msg: Buffer): Promise<Buffer> {
468
+ const authRequest = AuthRequest.fromBuffer(msg);
469
+ const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch(_ => undefined);
470
+ if (statusMessage === undefined) {
471
+ return Buffer.alloc(0);
472
+ }
473
+
474
+ // Find a validator address that is in the set
475
+ const allRegisteredValidators = await this.epochCache.getRegisteredValidators();
476
+ const addressToUse = this.getValidatorAddresses().find(
477
+ address => allRegisteredValidators.find(v => v.equals(address)) !== undefined,
478
+ );
479
+ if (addressToUse === undefined) {
480
+ // We don't have a registered address
481
+ return Buffer.alloc(0);
482
+ }
483
+
484
+ const payloadToSign = authRequest.getPayloadToSign();
485
+ const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign);
486
+ const authResponse = new AuthResponse(statusMessage, signature);
487
+ return authResponse.toBuffer();
361
488
  }
362
489
  }