@aztec/validator-client 0.0.0-test.1 → 0.0.1-commit.03f7ef2

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