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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/README.md +325 -0
  2. package/dest/checkpoint_builder.d.ts +79 -0
  3. package/dest/checkpoint_builder.d.ts.map +1 -0
  4. package/dest/checkpoint_builder.js +251 -0
  5. package/dest/config.d.ts +3 -14
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +75 -12
  8. package/dest/duties/validation_service.d.ts +49 -13
  9. package/dest/duties/validation_service.d.ts.map +1 -1
  10. package/dest/duties/validation_service.js +112 -18
  11. package/dest/factory.d.ts +34 -6
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +19 -6
  14. package/dest/index.d.ts +5 -2
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +4 -1
  17. package/dest/key_store/ha_key_store.d.ts +99 -0
  18. package/dest/key_store/ha_key_store.d.ts.map +1 -0
  19. package/dest/key_store/ha_key_store.js +208 -0
  20. package/dest/key_store/index.d.ts +4 -1
  21. package/dest/key_store/index.d.ts.map +1 -1
  22. package/dest/key_store/index.js +3 -0
  23. package/dest/key_store/interface.d.ts +85 -6
  24. package/dest/key_store/interface.d.ts.map +1 -1
  25. package/dest/key_store/interface.js +3 -3
  26. package/dest/key_store/local_key_store.d.ts +46 -11
  27. package/dest/key_store/local_key_store.d.ts.map +1 -1
  28. package/dest/key_store/local_key_store.js +68 -17
  29. package/dest/key_store/node_keystore_adapter.d.ts +151 -0
  30. package/dest/key_store/node_keystore_adapter.d.ts.map +1 -0
  31. package/dest/key_store/node_keystore_adapter.js +330 -0
  32. package/dest/key_store/web3signer_key_store.d.ts +66 -0
  33. package/dest/key_store/web3signer_key_store.d.ts.map +1 -0
  34. package/dest/key_store/web3signer_key_store.js +156 -0
  35. package/dest/metrics.d.ts +25 -5
  36. package/dest/metrics.d.ts.map +1 -1
  37. package/dest/metrics.js +86 -21
  38. package/dest/proposal_handler.d.ts +134 -0
  39. package/dest/proposal_handler.d.ts.map +1 -0
  40. package/dest/proposal_handler.js +1072 -0
  41. package/dest/validator.d.ts +109 -59
  42. package/dest/validator.d.ts.map +1 -1
  43. package/dest/validator.js +698 -171
  44. package/package.json +37 -21
  45. package/src/checkpoint_builder.ts +417 -0
  46. package/src/config.ts +87 -26
  47. package/src/duties/validation_service.ts +200 -21
  48. package/src/factory.ts +82 -11
  49. package/src/index.ts +4 -1
  50. package/src/key_store/ha_key_store.ts +269 -0
  51. package/src/key_store/index.ts +3 -0
  52. package/src/key_store/interface.ts +100 -5
  53. package/src/key_store/local_key_store.ts +77 -18
  54. package/src/key_store/node_keystore_adapter.ts +398 -0
  55. package/src/key_store/web3signer_key_store.ts +205 -0
  56. package/src/metrics.ts +121 -22
  57. package/src/proposal_handler.ts +1161 -0
  58. package/src/validator.ts +975 -222
  59. package/dest/errors/index.d.ts +0 -2
  60. package/dest/errors/index.d.ts.map +0 -1
  61. package/dest/errors/index.js +0 -1
  62. package/dest/errors/validator.error.d.ts +0 -29
  63. package/dest/errors/validator.error.d.ts.map +0 -1
  64. package/dest/errors/validator.error.js +0 -45
  65. package/src/errors/index.ts +0 -1
  66. package/src/errors/validator.error.ts +0 -55
package/dest/validator.js CHANGED
@@ -1,225 +1,737 @@
1
- import { Buffer32 } from '@aztec/foundation/buffer';
1
+ import { getBlobsPerL1Block } from '@aztec/blob-lib';
2
+ import { CheckpointNumber } from '@aztec/foundation/branded-types';
3
+ import { FifoSet } from '@aztec/foundation/fifo-set';
2
4
  import { createLogger } from '@aztec/foundation/log';
3
5
  import { RunningPromise } from '@aztec/foundation/running-promise';
4
6
  import { sleep } from '@aztec/foundation/sleep';
5
7
  import { DateProvider } from '@aztec/foundation/timer';
6
- import { BlockProposalValidator } from '@aztec/p2p/msg_validators';
7
- import { WithTracer, getTelemetryClient } from '@aztec/telemetry-client';
8
+ import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
9
+ import { OffenseType, WANT_TO_CLEAR_SLASH_EVENT, WANT_TO_SLASH_EVENT, getOffenseTypeName } from '@aztec/slasher';
10
+ import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
11
+ import { AttestationTimeoutError } from '@aztec/stdlib/validators';
12
+ import { getTelemetryClient } from '@aztec/telemetry-client';
13
+ import { createHASigner, createLocalSignerWithProtection, createSignerFromSharedDb } from '@aztec/validator-ha-signer/factory';
14
+ import { DutyType } from '@aztec/validator-ha-signer/types';
15
+ import { EventEmitter } from 'events';
8
16
  import { ValidationService } from './duties/validation_service.js';
9
- import { AttestationTimeoutError, BlockBuilderNotProvidedError, InvalidValidatorPrivateKeyError, ReExFailedTxsError, ReExStateMismatchError, ReExTimeoutError, TransactionsNotAvailableError } from './errors/validator.error.js';
10
- import { LocalKeyStore } from './key_store/local_key_store.js';
17
+ import { HAKeyStore } from './key_store/ha_key_store.js';
18
+ import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
11
19
  import { ValidatorMetrics } from './metrics.js';
20
+ import { ProposalHandler } from './proposal_handler.js';
21
+ // We maintain a set of proposers who have proposed invalid blocks.
22
+ // Just cap the set to avoid unbounded growth.
23
+ const MAX_PROPOSERS_OF_INVALID_BLOCKS = 1000;
24
+ const MAX_TRACKED_INVALID_PROPOSAL_SLOTS = 1000;
25
+ const MAX_TRACKED_INVALID_CHECKPOINT_PROPOSALS = 1000;
26
+ const MAX_TRACKED_BAD_ATTESTATIONS = 10_000;
27
+ // What errors from the block proposal handler result in slashing
28
+ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
29
+ 'state_mismatch',
30
+ 'failed_txs',
31
+ 'global_variables_mismatch',
32
+ 'invalid_proposal',
33
+ 'parent_block_wrong_slot',
34
+ 'in_hash_mismatch'
35
+ ];
36
+ const SLASHABLE_CHECKPOINT_PROPOSAL_VALIDATION_RESULT = {
37
+ // enabled
38
+ ['invalid_fee_asset_price_modifier']: true,
39
+ ['checkpoint_header_mismatch']: true,
40
+ // These late mismatches should normally be caught by earlier checks, but if reached after validating the local
41
+ // checkpoint inputs, the proposer-signed payload disagrees with deterministic recomputation.
42
+ ['archive_mismatch']: true,
43
+ ['out_hash_mismatch']: true,
44
+ ['no_blocks_for_slot']: true,
45
+ ['too_many_blocks_in_checkpoint']: true,
46
+ ['checkpoint_validation_failed']: true,
47
+ ['last_block_archive_mismatch']: true,
48
+ // disabled
49
+ ['invalid_signature']: false,
50
+ ['last_block_not_found']: false,
51
+ ['block_fetch_error']: false,
52
+ ['checkpoint_already_published']: false
53
+ };
12
54
  /**
13
55
  * Validator Client
14
- */ export class ValidatorClient extends WithTracer {
56
+ */ export class ValidatorClient extends EventEmitter {
15
57
  keyStore;
16
58
  epochCache;
17
59
  p2pClient;
60
+ proposalHandler;
61
+ blockSource;
62
+ checkpointsBuilder;
63
+ worldState;
64
+ l1ToL2MessageSource;
18
65
  config;
66
+ blobClient;
67
+ slashingProtectionSigner;
19
68
  dateProvider;
20
- log;
69
+ tracer;
21
70
  validationService;
22
71
  metrics;
23
- // Used to check if we are sending the same proposal twice
24
- previousProposal;
25
- // Callback registered to: sequencer.buildBlock
26
- blockBuilder;
72
+ log;
73
+ // Whether it has already registered handlers on the p2p client
74
+ hasRegisteredHandlers;
75
+ /** Tracks the last block proposal we created, to detect duplicate proposal attempts. */ lastProposedBlock;
76
+ /** Tracks the last checkpoint proposal we created. */ lastProposedCheckpoint;
77
+ lastEpochForCommitteeUpdateLoop;
27
78
  epochCacheUpdateLoop;
28
- blockProposalValidator;
29
- constructor(keyStore, epochCache, p2pClient, config, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
30
- // Instantiate tracer
31
- super(telemetry, 'Validator'), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.config = config, this.dateProvider = dateProvider, this.log = log, this.blockBuilder = undefined;
79
+ /** Tracks the last epoch in which each attester successfully submitted at least one attestation. */ lastAttestedEpochByAttester;
80
+ proposersOfInvalidBlocks;
81
+ slotsWithInvalidProposals;
82
+ invalidCheckpointProposalOffenseKeys;
83
+ badAttestationOffenseKeys;
84
+ slotsWithProposalEquivocation;
85
+ /** Tracks the last checkpoint proposal we attested to, to prevent equivocation. */ lastAttestedProposal;
86
+ constructor(keyStore, epochCache, p2pClient, proposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, slashingProtectionSigner, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
87
+ super(), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.proposalHandler = proposalHandler, this.blockSource = blockSource, this.checkpointsBuilder = checkpointsBuilder, this.worldState = worldState, this.l1ToL2MessageSource = l1ToL2MessageSource, this.config = config, this.blobClient = blobClient, this.slashingProtectionSigner = slashingProtectionSigner, this.dateProvider = dateProvider, this.hasRegisteredHandlers = false, this.lastAttestedEpochByAttester = new Map(), this.proposersOfInvalidBlocks = FifoSet.withLimit(MAX_PROPOSERS_OF_INVALID_BLOCKS), this.slotsWithInvalidProposals = FifoSet.withLimit(MAX_TRACKED_INVALID_PROPOSAL_SLOTS), this.invalidCheckpointProposalOffenseKeys = FifoSet.withLimit(MAX_TRACKED_INVALID_CHECKPOINT_PROPOSALS), this.badAttestationOffenseKeys = FifoSet.withLimit(MAX_TRACKED_BAD_ATTESTATIONS), this.slotsWithProposalEquivocation = FifoSet.withLimit(MAX_TRACKED_INVALID_PROPOSAL_SLOTS);
88
+ // Create child logger with fisherman prefix if in fisherman mode
89
+ this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
90
+ this.tracer = telemetry.getTracer('Validator');
32
91
  this.metrics = new ValidatorMetrics(telemetry);
33
- this.validationService = new ValidationService(keyStore);
34
- this.blockProposalValidator = new BlockProposalValidator(epochCache);
35
- // Refresh epoch cache every second to trigger commiteeChanged event
36
- this.epochCacheUpdateLoop = new RunningPromise(()=>this.epochCache.getCommittee().then(()=>{}).catch((err)=>log.error('Error updating validator committee', err)), log, 1000);
37
- // Listen to commiteeChanged event to alert operator when their validator has entered the committee
38
- this.epochCache.on('committeeChanged', (newCommittee, epochNumber)=>{
39
- const me = this.keyStore.getAddress();
40
- if (newCommittee.some((addr)=>addr.equals(me))) {
41
- this.log.info(`Validator ${me.toString()} is on the validator committee for epoch ${epochNumber}`);
42
- } else {
43
- this.log.verbose(`Validator ${me.toString()} not on the validator committee for epoch ${epochNumber}`);
92
+ this.validationService = new ValidationService(keyStore, this.getSignatureContext(), this.log.createChild('validation-service'));
93
+ this.proposalHandler.setCheckpointProposalValidationFailureCallback((proposal, result, proposalInfo)=>this.handleInvalidCheckpointProposal(proposal, result, proposalInfo));
94
+ // Refresh epoch cache every second to trigger alert if participation in committee changes
95
+ this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), this.log, 1000);
96
+ const myAddresses = this.getValidatorAddresses();
97
+ this.log.verbose(`Initialized validator with addresses: ${myAddresses.map((a)=>a.toString()).join(', ')}`);
98
+ }
99
+ static validateKeyStoreConfiguration(keyStoreManager, logger) {
100
+ const validatorKeyStore = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
101
+ const validatorAddresses = validatorKeyStore.getAddresses();
102
+ // Verify that we can retrieve all required data from the key store
103
+ for (const address of validatorAddresses){
104
+ // Functions throw if required data is not available
105
+ let coinbase;
106
+ let feeRecipient;
107
+ try {
108
+ coinbase = validatorKeyStore.getCoinbaseAddress(address);
109
+ feeRecipient = validatorKeyStore.getFeeRecipient(address);
110
+ } catch (error) {
111
+ throw new Error(`Failed to retrieve required data for validator address ${address}, error: ${error}`);
44
112
  }
45
- });
46
- this.log.verbose(`Initialized validator with address ${this.keyStore.getAddress().toString()}`);
113
+ const publisherAddresses = validatorKeyStore.getPublisherAddresses(address);
114
+ if (!publisherAddresses.length) {
115
+ throw new Error(`No publisher addresses found for validator address ${address}`);
116
+ }
117
+ logger?.debug(`Validator ${address.toString()} configured with coinbase ${coinbase.toString()}, feeRecipient ${feeRecipient.toString()} and publishers ${publisherAddresses.map((x)=>x.toString()).join()}`);
118
+ }
47
119
  }
48
- static new(config, epochCache, p2pClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient()) {
49
- if (!config.validatorPrivateKey) {
50
- throw new InvalidValidatorPrivateKeyError();
120
+ async handleEpochCommitteeUpdate() {
121
+ try {
122
+ const { committee, epoch } = await this.epochCache.getCommittee('next');
123
+ if (!committee) {
124
+ this.log.trace(`No committee found for slot`);
125
+ return;
126
+ }
127
+ this.metrics.setCurrentEpoch(epoch);
128
+ if (epoch !== this.lastEpochForCommitteeUpdateLoop) {
129
+ const me = this.getValidatorAddresses();
130
+ const committeeSet = new Set(committee.map((v)=>v.toString()));
131
+ const inCommittee = me.filter((a)=>committeeSet.has(a.toString()));
132
+ if (inCommittee.length > 0) {
133
+ this.log.info(`Validators ${inCommittee.map((a)=>a.toString()).join(',')} are on the validator committee for epoch ${epoch}`);
134
+ } else {
135
+ this.log.verbose(`Validators ${me.map((a)=>a.toString()).join(', ')} are not on the validator committee for epoch ${epoch}`);
136
+ }
137
+ this.lastEpochForCommitteeUpdateLoop = epoch;
138
+ }
139
+ } catch (err) {
140
+ this.log.error(`Error updating epoch committee`, err);
141
+ }
142
+ }
143
+ static async new(config, checkpointsBuilder, worldState, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, blobClient, reexecutionTracker, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), slashingProtectionDb) {
144
+ const metrics = new ValidatorMetrics(telemetry);
145
+ const blockProposalValidator = new BlockProposalValidator(epochCache, {
146
+ txsPermitted: !config.disableTransactions,
147
+ maxTxsPerBlock: config.validateMaxTxsPerBlock,
148
+ maxBlocksPerCheckpoint: config.maxBlocksPerCheckpoint,
149
+ skipSlotValidation: config.skipProposalSlotValidation,
150
+ signatureContext: {
151
+ chainId: config.l1ChainId,
152
+ rollupAddress: config.rollupAddress
153
+ }
154
+ });
155
+ const proposalHandler = new ProposalHandler(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, epochCache, config, blobClient, reexecutionTracker, metrics, dateProvider, telemetry, undefined);
156
+ const nodeKeystoreAdapter = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
157
+ let slashingProtectionSigner;
158
+ if (slashingProtectionDb) {
159
+ // Shared database mode: use a pre-existing database (e.g. for testing HA setups).
160
+ ({ signer: slashingProtectionSigner } = createSignerFromSharedDb(slashingProtectionDb, config, {
161
+ telemetryClient: telemetry,
162
+ dateProvider
163
+ }));
164
+ } else if (config.haSigningEnabled) {
165
+ // Multi-node HA mode: use PostgreSQL-backed distributed locking.
166
+ // If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
167
+ const haConfig = {
168
+ ...config,
169
+ maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000
170
+ };
171
+ ({ signer: slashingProtectionSigner } = await createHASigner(haConfig, {
172
+ telemetryClient: telemetry,
173
+ dateProvider
174
+ }));
175
+ } else {
176
+ // Single-node mode: use LMDB-backed local signing protection.
177
+ // This prevents double-signing if the node crashes and restarts mid-proposal.
178
+ ({ signer: slashingProtectionSigner } = await createLocalSignerWithProtection(config, {
179
+ telemetryClient: telemetry,
180
+ dateProvider
181
+ }));
51
182
  }
52
- const privateKey = validatePrivateKey(config.validatorPrivateKey);
53
- const localKeyStore = new LocalKeyStore(privateKey);
54
- const validator = new ValidatorClient(localKeyStore, epochCache, p2pClient, config, dateProvider, telemetry);
55
- validator.registerBlockProposalHandler();
183
+ const validatorKeyStore = new HAKeyStore(nodeKeystoreAdapter, slashingProtectionSigner);
184
+ const validator = new ValidatorClient(validatorKeyStore, epochCache, p2pClient, proposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, slashingProtectionSigner, dateProvider, telemetry);
56
185
  return validator;
57
186
  }
187
+ getValidatorAddresses() {
188
+ return this.keyStore.getAddresses().filter((addr)=>!this.config.disabledValidators.some((disabled)=>disabled.equals(addr)));
189
+ }
190
+ getProposalHandler() {
191
+ return this.proposalHandler;
192
+ }
193
+ signWithAddress(addr, msg, context) {
194
+ return this.keyStore.signTypedDataWithAddress(addr, msg, context);
195
+ }
196
+ getSignatureContext() {
197
+ return {
198
+ chainId: this.config.l1ChainId,
199
+ rollupAddress: this.config.rollupAddress
200
+ };
201
+ }
202
+ getCoinbaseForAttestor(attestor) {
203
+ return this.keyStore.getCoinbaseAddress(attestor);
204
+ }
205
+ getFeeRecipientForAttestor(attestor) {
206
+ return this.keyStore.getFeeRecipient(attestor);
207
+ }
208
+ getConfig() {
209
+ return this.config;
210
+ }
211
+ hasProposalEquivocation(slotNumber) {
212
+ return this.slotsWithProposalEquivocation.has(slotNumber);
213
+ }
214
+ hasInvalidProposals(slotNumber) {
215
+ return this.slotsWithInvalidProposals.has(slotNumber);
216
+ }
217
+ updateConfig(config) {
218
+ this.config = {
219
+ ...this.config,
220
+ ...config
221
+ };
222
+ this.proposalHandler.updateConfig(config);
223
+ }
224
+ reloadKeystore(newManager) {
225
+ const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
226
+ this.keyStore = new HAKeyStore(newAdapter, this.slashingProtectionSigner);
227
+ this.validationService = new ValidationService(this.keyStore, this.getSignatureContext(), this.log.createChild('validation-service'));
228
+ }
58
229
  async start() {
59
- // Sync the committee from the smart contract
60
- // https://github.com/AztecProtocol/aztec-packages/issues/7962
61
- const me = this.keyStore.getAddress();
62
- const inCommittee = await this.epochCache.isInCommittee(me);
63
- if (inCommittee) {
64
- this.log.info(`Started validator with address ${me.toString()} in current validator committee`);
65
- } else {
66
- this.log.info(`Started validator with address ${me.toString()}`);
230
+ if (this.epochCacheUpdateLoop.isRunning()) {
231
+ this.log.warn(`Validator client already started`);
232
+ return;
233
+ }
234
+ await this.keyStore.start();
235
+ await this.registerHandlers();
236
+ const myAddresses = this.getValidatorAddresses();
237
+ const inCommittee = await this.epochCache.filterInCommittee('now', myAddresses);
238
+ this.log.info(`Started validator with addresses: ${myAddresses.map((a)=>a.toString()).join(', ')}`);
239
+ if (inCommittee.length > 0) {
240
+ this.log.info(`Addresses in current validator committee: ${inCommittee.map((a)=>a.toString()).join(', ')}`);
67
241
  }
68
242
  this.epochCacheUpdateLoop.start();
69
243
  return Promise.resolve();
70
244
  }
71
245
  async stop() {
72
246
  await this.epochCacheUpdateLoop.stop();
247
+ await this.keyStore.stop();
73
248
  }
74
- registerBlockProposalHandler() {
75
- const handler = (block)=>{
76
- return this.attestToProposal(block);
77
- };
78
- this.p2pClient.registerBlockProposalHandler(handler);
249
+ /** Register handlers on the p2p client */ async registerHandlers() {
250
+ if (!this.hasRegisteredHandlers) {
251
+ this.hasRegisteredHandlers = true;
252
+ this.log.debug(`Registering validator handlers for p2p client`);
253
+ // Block proposal handler - validates but does NOT attest (validators only attest to checkpoints)
254
+ const blockHandler = (block, proposalSender)=>this.validateBlockProposal(block, proposalSender);
255
+ this.p2pClient.registerBlockProposalHandler(blockHandler);
256
+ // Checkpoint proposal handler - validates and creates attestations
257
+ // The checkpoint is received as CheckpointProposalCore since the lastBlock is extracted
258
+ // and processed separately via the block handler above.
259
+ const checkpointHandler = (checkpoint, proposalSender)=>this.attestToCheckpointProposal(checkpoint, proposalSender);
260
+ this.p2pClient.registerValidatorCheckpointProposalHandler(checkpointHandler);
261
+ // Duplicate proposal handler - triggers slashing for equivocation
262
+ this.p2pClient.registerDuplicateProposalCallback((info)=>{
263
+ this.handleDuplicateProposal(info);
264
+ });
265
+ // Duplicate attestation handler - triggers slashing for attestation equivocation
266
+ this.p2pClient.registerDuplicateAttestationCallback((info)=>{
267
+ this.handleDuplicateAttestation(info);
268
+ });
269
+ this.p2pClient.registerCheckpointAttestationCallback((attestation)=>{
270
+ this.handleCheckpointAttestation(attestation);
271
+ });
272
+ const myAddresses = this.getValidatorAddresses();
273
+ this.p2pClient.registerThisValidatorAddresses(myAddresses);
274
+ await this.p2pClient.addReqRespSubProtocol(ReqRespSubProtocol.AUTH, this.handleAuthRequest.bind(this));
275
+ }
79
276
  }
80
277
  /**
81
- * Register a callback function for building a block
82
- *
83
- * We reuse the sequencer's block building functionality for re-execution
84
- */ registerBlockBuilder(blockBuilder) {
85
- this.blockBuilder = blockBuilder;
86
- }
87
- async attestToProposal(proposal) {
88
- const slotNumber = proposal.slotNumber.toNumber();
278
+ * Validate a block proposal from a peer.
279
+ * Note: Validators do NOT attest to individual blocks - attestations are only for checkpoint proposals.
280
+ * @returns true if the proposal is valid, false otherwise
281
+ */ async validateBlockProposal(proposal, proposalSender) {
282
+ const slotNumber = proposal.slotNumber;
283
+ // Note: During escape hatch, we still want to "validate" proposals for observability,
284
+ // but we intentionally reject them and disable slashing invalid block and attestation flow.
285
+ const escapeHatchOpen = await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber);
286
+ const proposer = proposal.getSender();
287
+ // Reject proposals with invalid signatures
288
+ if (!proposer) {
289
+ this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
290
+ return false;
291
+ }
292
+ // Log self-proposals from HA peers (same validator key on different nodes)
293
+ if (this.getValidatorAddresses().some((addr)=>addr.equals(proposer))) {
294
+ this.log.verbose(`Processing block proposal from HA peer for slot ${slotNumber}`, {
295
+ proposer: proposer.toString(),
296
+ slotNumber
297
+ });
298
+ }
299
+ // Check if we're in the committee (for metrics purposes)
300
+ const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
301
+ const partOfCommittee = inCommittee.length > 0;
89
302
  const proposalInfo = {
90
- slotNumber,
91
- blockNumber: proposal.payload.header.globalVariables.blockNumber.toNumber(),
92
- archive: proposal.payload.archive.toString(),
93
- txCount: proposal.payload.txHashes.length,
94
- txHashes: proposal.payload.txHashes.map((txHash)=>txHash.toString())
303
+ ...proposal.toBlockInfo(),
304
+ proposer: proposer.toString()
95
305
  };
96
- this.log.verbose(`Received request to attest for slot ${slotNumber}`);
97
- // Check that I am in the committee
98
- if (!await this.epochCache.isInCommittee(this.keyStore.getAddress())) {
99
- this.log.verbose(`Not in the committee, skipping attestation`);
306
+ this.log.info(`Received block proposal for slot ${slotNumber}`, {
307
+ ...proposalInfo,
308
+ txHashes: proposal.txHashes.map((t)=>t.toString()),
309
+ fishermanMode: this.config.fishermanMode || false
310
+ });
311
+ // Reexecute outside the escape hatch so slashing observers can detect invalid proposals even when penalties are 0.
312
+ const validationResult = await this.proposalHandler.handleBlockProposal(proposal, proposalSender, !escapeHatchOpen);
313
+ if (!validationResult.isValid) {
314
+ const reason = validationResult.reason || 'unknown';
315
+ this.log.warn(`Block proposal validation failed: ${reason}`, proposalInfo);
316
+ // Classify failure reason: bad proposal vs node issue
317
+ const badProposalReasons = [
318
+ 'invalid_proposal',
319
+ 'state_mismatch',
320
+ 'failed_txs',
321
+ 'in_hash_mismatch',
322
+ 'parent_block_wrong_slot'
323
+ ];
324
+ if (badProposalReasons.includes(reason)) {
325
+ this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
326
+ } else {
327
+ // Node issues so we can't validate
328
+ this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
329
+ }
330
+ if (!escapeHatchOpen && validationResult.reason && SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason)) {
331
+ this.log.info(`Detected invalid block proposal offense`, {
332
+ ...proposalInfo,
333
+ amount: this.config.slashBroadcastedInvalidBlockPenalty,
334
+ offenseType: getOffenseTypeName(OffenseType.BROADCASTED_INVALID_BLOCK_PROPOSAL)
335
+ });
336
+ this.slashInvalidBlock(proposal);
337
+ this.markInvalidProposalSlot(proposal.slotNumber);
338
+ }
339
+ return false;
340
+ }
341
+ this.log.info(`Validated block proposal for slot ${slotNumber}`, {
342
+ ...proposalInfo,
343
+ inCommittee: partOfCommittee,
344
+ fishermanMode: this.config.fishermanMode || false,
345
+ escapeHatchOpen
346
+ });
347
+ if (escapeHatchOpen) {
348
+ this.log.warn(`Escape hatch open for slot ${slotNumber}, rejecting block proposal`, proposalInfo);
349
+ return false;
350
+ }
351
+ return true;
352
+ }
353
+ /**
354
+ * Validate and attest to a checkpoint proposal from a peer.
355
+ * The proposal is received as CheckpointProposalCore (without lastBlock) since
356
+ * the lastBlock is extracted and processed separately via the block handler.
357
+ * @returns Checkpoint attestations if valid, undefined otherwise
358
+ */ async attestToCheckpointProposal(proposal, _proposalSender) {
359
+ const proposalSlotNumber = proposal.slotNumber;
360
+ const proposer = proposal.getSender();
361
+ // If escape hatch is open for this slot's epoch, do not attest.
362
+ if (await this.epochCache.isEscapeHatchOpenAtSlot(proposalSlotNumber)) {
363
+ this.log.warn(`Escape hatch open for slot ${proposalSlotNumber}, skipping checkpoint attestation handling`);
100
364
  return undefined;
101
365
  }
102
- // Check that the proposal is from the current proposer, or the next proposer.
103
- const invalidProposal = await this.blockProposalValidator.validate(proposal);
104
- if (invalidProposal) {
105
- this.log.verbose(`Proposal is not valid, skipping attestation`);
366
+ // Early-out for equivocation: refuses if we've already attested to a higher slot.
367
+ if (!this.shouldAttestToSlot(proposalSlotNumber)) {
106
368
  return undefined;
107
369
  }
108
- // Check that all of the transactions in the proposal are available in the tx pool before attesting
109
- this.log.verbose(`Processing attestation for slot ${slotNumber}`, proposalInfo);
110
- try {
111
- await this.ensureTransactionsAreAvailable(proposal);
112
- if (this.config.validatorReexecute) {
113
- this.log.verbose(`Re-executing transactions in the proposal before attesting`);
114
- await this.reExecuteTransactions(proposal);
115
- }
116
- } catch (error) {
117
- // If the transactions are not available, then we should not attempt to attest
118
- if (error instanceof TransactionsNotAvailableError) {
119
- this.log.error(`Transactions not available, skipping attestation`, error, proposalInfo);
120
- } else {
121
- // This branch most commonly be hit if the transactions are available, but the re-execution fails
122
- // Catch all error handler
123
- this.log.error(`Failed to attest to proposal`, error, proposalInfo);
370
+ // Ignore proposals from ourselves (may happen in HA setups)
371
+ if (proposer && this.getValidatorAddresses().some((addr)=>addr.equals(proposer))) {
372
+ this.log.debug(`Ignoring block proposal from self for slot ${proposalSlotNumber}`, {
373
+ proposer: proposer.toString(),
374
+ proposalSlotNumber
375
+ });
376
+ return undefined;
377
+ }
378
+ // Check that I have any address in the committee where this checkpoint will land before attesting
379
+ const inCommittee = await this.epochCache.filterInCommittee(proposalSlotNumber, this.getValidatorAddresses());
380
+ const partOfCommittee = inCommittee.length > 0;
381
+ const proposalInfo = {
382
+ proposalSlotNumber,
383
+ archive: proposal.archive.toString(),
384
+ proposer: proposer?.toString()
385
+ };
386
+ this.log.info(`Received checkpoint proposal for slot ${proposalSlotNumber}`, {
387
+ ...proposalInfo,
388
+ fishermanMode: this.config.fishermanMode || false
389
+ });
390
+ // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set).
391
+ // Uses the cached result from the all-nodes callback if available (avoids double validation).
392
+ let checkpointNumber;
393
+ if (this.config.skipCheckpointProposalValidation) {
394
+ this.log.warn(`Skipping checkpoint proposal validation for slot ${proposalSlotNumber}`, proposalInfo);
395
+ checkpointNumber = CheckpointNumber(0);
396
+ } else {
397
+ const validationResult = await this.proposalHandler.handleCheckpointProposal(proposal, proposalInfo);
398
+ if (!validationResult.isValid) {
399
+ this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
400
+ return undefined;
124
401
  }
402
+ checkpointNumber = validationResult.checkpointNumber;
403
+ }
404
+ // Check that I have any address in current committee before attesting
405
+ // In fisherman mode, we still create attestations for validation even if not in committee
406
+ if (!partOfCommittee && !this.config.fishermanMode) {
407
+ this.log.verbose(`No validator in the current committee, skipping attestation`, proposalInfo);
125
408
  return undefined;
126
409
  }
127
410
  // Provided all of the above checks pass, we can attest to the proposal
128
- this.log.info(`Attesting to proposal for slot ${slotNumber}`, proposalInfo);
129
- // If the above function does not throw an error, then we can attest to the proposal
130
- return this.validationService.attestToProposal(proposal);
411
+ this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${proposalSlotNumber}`, {
412
+ ...proposalInfo,
413
+ inCommittee: partOfCommittee,
414
+ fishermanMode: this.config.fishermanMode || false
415
+ });
416
+ this.metrics.incSuccessfulAttestations(inCommittee.length);
417
+ // Track epoch participation per attester: count each (attester, epoch) pair at most once
418
+ const proposalEpoch = getEpochAtSlot(proposalSlotNumber, this.epochCache.getL1Constants());
419
+ for (const attester of inCommittee){
420
+ const key = attester.toString();
421
+ const lastEpoch = this.lastAttestedEpochByAttester.get(key);
422
+ if (lastEpoch === undefined || proposalEpoch > lastEpoch) {
423
+ this.lastAttestedEpochByAttester.set(key, proposalEpoch);
424
+ this.metrics.incAttestedEpochCount(attester);
425
+ }
426
+ }
427
+ // Determine which validators should attest
428
+ let attestors;
429
+ if (partOfCommittee) {
430
+ attestors = inCommittee;
431
+ } else if (this.config.fishermanMode) {
432
+ // In fisherman mode, create attestations for validation purposes even if not in committee. These won't be broadcast.
433
+ attestors = this.getValidatorAddresses();
434
+ } else {
435
+ attestors = [];
436
+ }
437
+ // Only create attestations if we have attestors
438
+ if (attestors.length === 0) {
439
+ return undefined;
440
+ }
441
+ if (this.config.fishermanMode) {
442
+ // bail out early and don't save attestations to the pool in fisherman mode
443
+ this.log.info(`Creating checkpoint attestations for slot ${proposalSlotNumber}`, {
444
+ ...proposalInfo,
445
+ attestors: attestors.map((a)=>a.toString())
446
+ });
447
+ return undefined;
448
+ }
449
+ return await this.createCheckpointAttestationsFromProposal(proposal, attestors, checkpointNumber);
131
450
  }
132
451
  /**
133
- * Re-execute the transactions in the proposal and check that the state updates match the header state
134
- * @param proposal - The proposal to re-execute
135
- */ async reExecuteTransactions(proposal) {
136
- const { header, txHashes } = proposal.payload;
137
- const txs = (await Promise.all(txHashes.map((tx)=>this.p2pClient.getTxByHash(tx)))).filter((tx)=>tx !== undefined);
138
- // If we cannot request all of the transactions, then we should fail
139
- if (txs.length !== txHashes.length) {
140
- throw new TransactionsNotAvailableError(txHashes);
141
- }
142
- // Assertion: This check will fail if re-execution is not enabled
143
- if (this.blockBuilder === undefined) {
144
- throw new BlockBuilderNotProvidedError();
145
- }
146
- // Use the sequencer's block building logic to re-execute the transactions
147
- const stopTimer = this.metrics.reExecutionTimer();
148
- const { block, numFailedTxs } = await this.blockBuilder(txs, header.globalVariables, {
149
- validateOnly: true
150
- });
151
- stopTimer();
152
- this.log.verbose(`Transaction re-execution complete`);
153
- if (numFailedTxs > 0) {
154
- await this.metrics.recordFailedReexecution(proposal);
155
- throw new ReExFailedTxsError(numFailedTxs);
452
+ * Checks if we should attest to a slot based on equivocation prevention rules.
453
+ * @returns true if we should attest, false if we should skip
454
+ */ shouldAttestToSlot(slotNumber) {
455
+ // If attestToEquivocatedProposals is true, always allow
456
+ if (this.config.attestToEquivocatedProposals) {
457
+ return true;
156
458
  }
157
- if (block.body.txEffects.length !== txHashes.length) {
158
- await this.metrics.recordFailedReexecution(proposal);
159
- throw new ReExTimeoutError();
459
+ // Check if incoming slot is strictly greater than last attested
460
+ if (this.lastAttestedProposal && slotNumber <= this.lastAttestedProposal.slotNumber) {
461
+ this.log.warn(`Refusing to process a proposal for slot ${slotNumber} given we already attested to a proposal for slot ${this.lastAttestedProposal.slotNumber}`);
462
+ return false;
160
463
  }
161
- // This function will throw an error if state updates do not match
162
- if (!block.archive.root.equals(proposal.archive)) {
163
- await this.metrics.recordFailedReexecution(proposal);
164
- throw new ReExStateMismatchError();
464
+ return true;
465
+ }
466
+ async createCheckpointAttestationsFromProposal(proposal, attestors = [], checkpointNumber) {
467
+ // Equivocation check: must happen right before signing to minimize the race window
468
+ if (!this.shouldAttestToSlot(proposal.slotNumber)) {
469
+ return undefined;
165
470
  }
471
+ const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors, checkpointNumber);
472
+ // Track the proposal we attested to (to prevent equivocation)
473
+ this.lastAttestedProposal = proposal;
474
+ await this.p2pClient.addOwnCheckpointAttestations(attestations);
475
+ return attestations;
166
476
  }
167
477
  /**
168
- * Ensure that all of the transactions in the proposal are available in the tx pool before attesting
169
- *
170
- * 1. Check if the local tx pool contains all of the transactions in the proposal
171
- * 2. If any transactions are not in the local tx pool, request them from the network
172
- * 3. If we cannot retrieve them from the network, throw an error
173
- * @param proposal - The proposal to attest to
174
- */ async ensureTransactionsAreAvailable(proposal) {
175
- const txHashes = proposal.payload.txHashes;
176
- const transactionStatuses = await Promise.all(txHashes.map((txHash)=>this.p2pClient.getTxStatus(txHash)));
177
- const missingTxs = txHashes.filter((_, index)=>![
178
- 'pending',
179
- 'mined'
180
- ].includes(transactionStatuses[index] ?? ''));
181
- if (missingTxs.length === 0) {
182
- return; // All transactions are available
183
- }
184
- this.log.verbose(`Missing ${missingTxs.length} transactions in the tx pool, requesting from the network`);
185
- const requestedTxs = await this.p2pClient.requestTxs(missingTxs);
186
- if (requestedTxs.some((tx)=>tx === undefined)) {
187
- throw new TransactionsNotAvailableError(missingTxs);
188
- }
189
- }
190
- async createBlockProposal(header, archive, txs) {
191
- if (this.previousProposal?.slotNumber.equals(header.globalVariables.slotNumber)) {
192
- this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
193
- return Promise.resolve(undefined);
194
- }
195
- const newProposal = await this.validationService.createBlockProposal(header, archive, txs);
196
- this.previousProposal = newProposal;
478
+ * Uploads blobs for a checkpoint to the filestore (fire and forget).
479
+ */ async uploadBlobsForCheckpoint(proposal, proposalInfo) {
480
+ try {
481
+ const lastBlockHeader = (await this.blockSource.getBlockData({
482
+ archive: proposal.archive
483
+ }))?.header;
484
+ if (!lastBlockHeader) {
485
+ this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
486
+ return;
487
+ }
488
+ const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
489
+ if (blocks.length === 0) {
490
+ this.log.warn(`No blocks found for blob upload`, proposalInfo);
491
+ return;
492
+ }
493
+ const blobFields = blocks.flatMap((b)=>b.toBlobFields());
494
+ const blobs = await getBlobsPerL1Block(blobFields);
495
+ await this.blobClient.sendBlobsToFilestore(blobs);
496
+ this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
497
+ ...proposalInfo,
498
+ numBlobs: blobs.length
499
+ });
500
+ } catch (err) {
501
+ this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
502
+ }
503
+ }
504
+ slashInvalidBlock(proposal) {
505
+ const proposer = proposal.getSender();
506
+ // Skip if signature is invalid (shouldn't happen since we validate earlier)
507
+ if (!proposer) {
508
+ this.log.warn(`Cannot slash proposal with invalid signature`);
509
+ return;
510
+ }
511
+ this.proposersOfInvalidBlocks.add(proposer.toString());
512
+ this.emit(WANT_TO_SLASH_EVENT, [
513
+ {
514
+ validator: proposer,
515
+ amount: this.config.slashBroadcastedInvalidBlockPenalty,
516
+ offenseType: OffenseType.BROADCASTED_INVALID_BLOCK_PROPOSAL,
517
+ epochOrSlot: BigInt(proposal.slotNumber)
518
+ }
519
+ ]);
520
+ }
521
+ handleInvalidCheckpointProposal(proposal, result, proposalInfo) {
522
+ if (!SLASHABLE_CHECKPOINT_PROPOSAL_VALIDATION_RESULT[result.reason]) {
523
+ return;
524
+ }
525
+ this.markInvalidProposalSlot(proposal.slotNumber);
526
+ if (this.slashInvalidCheckpointProposal(proposal)) {
527
+ this.log.info(`Detected invalid checkpoint proposal offense`, {
528
+ ...proposalInfo,
529
+ reason: result.reason,
530
+ amount: this.config.slashBroadcastedInvalidCheckpointProposalPenalty,
531
+ offenseType: getOffenseTypeName(OffenseType.BROADCASTED_INVALID_CHECKPOINT_PROPOSAL)
532
+ });
533
+ }
534
+ }
535
+ slashInvalidCheckpointProposal(proposal) {
536
+ const proposer = proposal.getSender();
537
+ if (!proposer) {
538
+ this.log.warn(`Cannot slash checkpoint proposal with invalid signature`, {
539
+ slotNumber: proposal.slotNumber,
540
+ archive: proposal.archive.toString()
541
+ });
542
+ return false;
543
+ }
544
+ const offenseType = OffenseType.BROADCASTED_INVALID_CHECKPOINT_PROPOSAL;
545
+ const offenseKey = `${proposer.toString()}:${offenseType}:${proposal.slotNumber}`;
546
+ if (!this.invalidCheckpointProposalOffenseKeys.addIfAbsent(offenseKey)) {
547
+ return false;
548
+ }
549
+ this.emit(WANT_TO_SLASH_EVENT, [
550
+ {
551
+ validator: proposer,
552
+ amount: this.config.slashBroadcastedInvalidCheckpointProposalPenalty,
553
+ offenseType,
554
+ epochOrSlot: BigInt(proposal.slotNumber)
555
+ }
556
+ ]);
557
+ return true;
558
+ }
559
+ markInvalidProposalSlot(slotNumber) {
560
+ this.slotsWithInvalidProposals.add(slotNumber);
561
+ }
562
+ handleCheckpointAttestation(attestation) {
563
+ const slotNumber = attestation.slotNumber;
564
+ if (!this.slotsWithInvalidProposals.has(slotNumber) || this.slotsWithProposalEquivocation.has(slotNumber)) {
565
+ return;
566
+ }
567
+ const attester = attestation.getSender();
568
+ if (!attester) {
569
+ this.log.warn(`Cannot slash checkpoint attestation with invalid signature`, {
570
+ slotNumber,
571
+ archive: attestation.archive.toString()
572
+ });
573
+ return;
574
+ }
575
+ this.slashAttestedToInvalidCheckpointProposal(slotNumber, attester);
576
+ }
577
+ slashAttestedToInvalidCheckpointProposal(slotNumber, attester) {
578
+ const offenseKey = `${attester.toString()}:${OffenseType.ATTESTED_TO_INVALID_CHECKPOINT_PROPOSAL}:${slotNumber}`;
579
+ if (!this.badAttestationOffenseKeys.addIfAbsent(offenseKey)) {
580
+ return;
581
+ }
582
+ this.log.info(`Detected attestation to invalid checkpoint proposal offense`, {
583
+ attester: attester.toString(),
584
+ slotNumber,
585
+ amount: this.config.slashAttestInvalidCheckpointProposalPenalty,
586
+ offenseType: getOffenseTypeName(OffenseType.ATTESTED_TO_INVALID_CHECKPOINT_PROPOSAL)
587
+ });
588
+ this.emit(WANT_TO_SLASH_EVENT, [
589
+ {
590
+ validator: attester,
591
+ amount: this.config.slashAttestInvalidCheckpointProposalPenalty,
592
+ offenseType: OffenseType.ATTESTED_TO_INVALID_CHECKPOINT_PROPOSAL,
593
+ epochOrSlot: BigInt(slotNumber)
594
+ }
595
+ ]);
596
+ }
597
+ /**
598
+ * Handle detection of a duplicate proposal (equivocation).
599
+ * Emits a slash event when a proposer sends multiple proposals for the same position.
600
+ */ handleDuplicateProposal(info) {
601
+ const { slot, proposer, type } = info;
602
+ this.slotsWithProposalEquivocation.add(slot);
603
+ this.log.info(`Detected duplicate ${type} proposal offense from ${proposer.toString()} at slot ${slot}`, {
604
+ proposer: proposer.toString(),
605
+ slot,
606
+ type,
607
+ amount: this.config.slashDuplicateProposalPenalty,
608
+ offenseType: getOffenseTypeName(OffenseType.DUPLICATE_PROPOSAL)
609
+ });
610
+ this.emit(WANT_TO_SLASH_EVENT, [
611
+ {
612
+ validator: proposer,
613
+ amount: this.config.slashDuplicateProposalPenalty,
614
+ offenseType: OffenseType.DUPLICATE_PROPOSAL,
615
+ epochOrSlot: BigInt(slot)
616
+ }
617
+ ]);
618
+ this.emit(WANT_TO_CLEAR_SLASH_EVENT, [
619
+ {
620
+ offenseType: OffenseType.ATTESTED_TO_INVALID_CHECKPOINT_PROPOSAL,
621
+ epochOrSlot: BigInt(slot)
622
+ }
623
+ ]);
624
+ }
625
+ /**
626
+ * Handle detection of a duplicate attestation (equivocation).
627
+ * Emits a slash event when an attester signs attestations for different proposals at the same slot.
628
+ */ handleDuplicateAttestation(info) {
629
+ const { slot, attester } = info;
630
+ this.log.info(`Detected duplicate attestation offense from ${attester.toString()} at slot ${slot}`, {
631
+ attester: attester.toString(),
632
+ slot,
633
+ amount: this.config.slashDuplicateAttestationPenalty,
634
+ offenseType: getOffenseTypeName(OffenseType.DUPLICATE_ATTESTATION)
635
+ });
636
+ this.emit(WANT_TO_SLASH_EVENT, [
637
+ {
638
+ validator: attester,
639
+ amount: this.config.slashDuplicateAttestationPenalty,
640
+ offenseType: OffenseType.DUPLICATE_ATTESTATION,
641
+ epochOrSlot: BigInt(slot)
642
+ }
643
+ ]);
644
+ }
645
+ async createBlockProposal(blockHeader, checkpointNumber, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, options = {}) {
646
+ // Validate that we're not creating a proposal for an older or equal position
647
+ if (this.lastProposedBlock) {
648
+ const lastSlot = this.lastProposedBlock.slotNumber;
649
+ const lastIndex = this.lastProposedBlock.indexWithinCheckpoint;
650
+ const newSlot = blockHeader.globalVariables.slotNumber;
651
+ if (newSlot < lastSlot || newSlot === lastSlot && indexWithinCheckpoint <= lastIndex) {
652
+ throw new Error(`Cannot create block proposal for slot ${newSlot} index ${indexWithinCheckpoint}: ` + `already proposed block for slot ${lastSlot} index ${lastIndex}`);
653
+ }
654
+ }
655
+ this.log.info(`Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`);
656
+ const newProposal = await this.validationService.createBlockProposal(blockHeader, checkpointNumber, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, {
657
+ ...options,
658
+ broadcastInvalidBlockProposal: options.broadcastInvalidBlockProposal || this.config.broadcastInvalidBlockProposal
659
+ });
660
+ this.lastProposedBlock = newProposal;
197
661
  return newProposal;
198
662
  }
199
- broadcastBlockProposal(proposal) {
200
- this.p2pClient.broadcastProposal(proposal);
663
+ async createCheckpointProposal(checkpointHeader, archive, checkpointNumber, feeAssetPriceModifier, lastBlockProposal, proposerAddress, options = {}) {
664
+ // Validate that we're not creating a proposal for an older or equal slot
665
+ if (this.lastProposedCheckpoint) {
666
+ const lastSlot = this.lastProposedCheckpoint.slotNumber;
667
+ const newSlot = checkpointHeader.slotNumber;
668
+ if (newSlot <= lastSlot) {
669
+ throw new Error(`Cannot create checkpoint proposal for slot ${newSlot}: ` + `already proposed checkpoint for slot ${lastSlot}`);
670
+ }
671
+ }
672
+ this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
673
+ const newProposal = await this.validationService.createCheckpointProposal(checkpointHeader, archive, checkpointNumber, feeAssetPriceModifier, lastBlockProposal, proposerAddress, options);
674
+ this.lastProposedCheckpoint = newProposal;
675
+ // Self-record this slot's outcome on the re-execution tracker. Proposers don't run their
676
+ // own proposals through `handleCheckpointProposal`, so without this call the proposer's
677
+ // sentinel would see no outcome for slots it proposed and would mis-attribute itself as
678
+ // inactive. We pass the locally-computed `archive` (not `newProposal.archive`, which may
679
+ // be intentionally corrupted under test-only flags); from the proposer's local-view
680
+ // perspective the work it just completed is valid by definition.
681
+ this.proposalHandler.recordOwnCheckpointProposalAsValid(checkpointHeader.slotNumber, archive, checkpointNumber);
682
+ return newProposal;
683
+ }
684
+ async broadcastBlockProposal(proposal) {
685
+ await this.p2pClient.broadcastProposal(proposal);
686
+ }
687
+ async signAttestationsAndSigners(attestationsAndSigners, proposer, slot, checkpointNumber) {
688
+ return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, checkpointNumber);
689
+ }
690
+ async collectOwnAttestations(proposal, checkpointNumber) {
691
+ const slot = proposal.slotNumber;
692
+ const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
693
+ this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, {
694
+ inCommittee
695
+ });
696
+ const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee, checkpointNumber);
697
+ if (!attestations) {
698
+ return [];
699
+ }
700
+ // We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
701
+ // other nodes can see that our validators did attest to this block proposal, and do not slash us
702
+ // due to inactivity for missed attestations.
703
+ void this.p2pClient.broadcastCheckpointAttestations(attestations).catch((err)=>{
704
+ this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
705
+ });
706
+ return attestations;
201
707
  }
202
- // TODO(https://github.com/AztecProtocol/aztec-packages/issues/7962)
203
- async collectAttestations(proposal, required, deadline) {
204
- // Wait and poll the p2pClient's attestation pool for this block until we have enough attestations
205
- const slot = proposal.payload.header.globalVariables.slotNumber.toBigInt();
708
+ async collectAttestations(proposal, required, deadline, checkpointNumber) {
709
+ // Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
710
+ const slot = proposal.slotNumber;
206
711
  this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
207
712
  if (+deadline < this.dateProvider.now()) {
208
713
  this.log.error(`Deadline ${deadline.toISOString()} for collecting ${required} attestations for slot ${slot} is in the past`);
209
- throw new AttestationTimeoutError(required, slot);
714
+ throw new AttestationTimeoutError(0, required, slot);
210
715
  }
211
- const proposalId = proposal.archive.toString();
212
- const myAttestation = await this.validationService.attestToProposal(proposal);
716
+ await this.collectOwnAttestations(proposal, checkpointNumber);
717
+ const proposalPayloadHash = proposal.getPayloadHash();
718
+ const myAddresses = this.getValidatorAddresses();
213
719
  let attestations = [];
214
720
  while(true){
215
- const collectedAttestations = [
216
- myAttestation,
217
- ...await this.p2pClient.getAttestationsForSlot(slot, proposalId)
218
- ];
219
- const oldSenders = await Promise.all(attestations.map((attestation)=>attestation.getSender()));
721
+ // The pool already filters by proposal payload hash; if any attestation slips through with a
722
+ // mismatched payload hash, drop it defensively. Equivocations are emitted as separate slash
723
+ // events from libp2p_service.
724
+ const collectedAttestations = await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalPayloadHash);
725
+ // Log new attestations we collected
726
+ const oldSenders = attestations.map((attestation)=>attestation.getSender());
220
727
  for (const collected of collectedAttestations){
221
- const collectedSender = await collected.getSender();
222
- if (!oldSenders.some((sender)=>sender.equals(collectedSender))) {
728
+ const collectedSender = collected.getSender();
729
+ // Skip attestations with invalid signatures. Should not happen as we don't add invalid attestations to our pool.
730
+ if (!collectedSender) {
731
+ this.log.warn(`Skipping attestation with invalid signature for slot ${slot}`);
732
+ continue;
733
+ }
734
+ if (!myAddresses.some((address)=>address.equals(collectedSender)) && !oldSenders.some((sender)=>sender?.equals(collectedSender))) {
223
735
  this.log.debug(`Received attestation for slot ${slot} from ${collectedSender.toString()}`);
224
736
  }
225
737
  }
@@ -230,17 +742,32 @@ import { ValidatorMetrics } from './metrics.js';
230
742
  }
231
743
  if (+deadline < this.dateProvider.now()) {
232
744
  this.log.error(`Timeout ${deadline.toISOString()} waiting for ${required} attestations for slot ${slot}`);
233
- throw new AttestationTimeoutError(required, slot);
745
+ throw new AttestationTimeoutError(attestations.length, required, slot);
234
746
  }
235
- this.log.debug(`Collected ${attestations.length} attestations so far`);
747
+ this.log.debug(`Collected ${attestations.length} of ${required} attestations so far`);
236
748
  await sleep(this.config.attestationPollingIntervalMs);
237
749
  }
238
750
  }
239
- }
240
- function validatePrivateKey(privateKey) {
241
- try {
242
- return Buffer32.fromString(privateKey);
243
- } catch (error) {
244
- throw new InvalidValidatorPrivateKeyError();
751
+ async handleAuthRequest(peer, msg) {
752
+ const authRequest = AuthRequest.fromBuffer(msg);
753
+ const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch((_)=>undefined);
754
+ if (statusMessage === undefined) {
755
+ return Buffer.alloc(0);
756
+ }
757
+ // Find a validator address that is in the set
758
+ const allRegisteredValidators = await this.epochCache.getRegisteredValidators();
759
+ const addressToUse = this.getValidatorAddresses().find((address)=>allRegisteredValidators.find((v)=>v.equals(address)) !== undefined);
760
+ if (addressToUse === undefined) {
761
+ // We don't have a registered address
762
+ return Buffer.alloc(0);
763
+ }
764
+ const payloadToSign = authRequest.getPayloadToSign();
765
+ // AUTH_REQUEST doesn't require HA protection - multiple signatures are safe
766
+ const context = {
767
+ dutyType: DutyType.AUTH_REQUEST
768
+ };
769
+ const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign, context);
770
+ const authResponse = new AuthResponse(statusMessage, signature);
771
+ return authResponse.toBuffer();
245
772
  }
246
773
  }