@aztec/validator-client 0.0.1-commit.5daedc8 → 0.0.1-commit.6201a7b05
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.
- package/README.md +324 -0
- package/dest/checkpoint_builder.d.ts +79 -0
- package/dest/checkpoint_builder.d.ts.map +1 -0
- package/dest/checkpoint_builder.js +251 -0
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +44 -14
- package/dest/duties/validation_service.d.ts +43 -15
- package/dest/duties/validation_service.d.ts.map +1 -1
- package/dest/duties/validation_service.js +96 -31
- package/dest/factory.d.ts +19 -11
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +10 -5
- package/dest/index.d.ts +3 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +2 -1
- package/dest/key_store/ha_key_store.d.ts +99 -0
- package/dest/key_store/ha_key_store.d.ts.map +1 -0
- package/dest/key_store/ha_key_store.js +208 -0
- package/dest/key_store/index.d.ts +2 -1
- package/dest/key_store/index.d.ts.map +1 -1
- package/dest/key_store/index.js +1 -0
- package/dest/key_store/interface.d.ts +36 -6
- package/dest/key_store/interface.d.ts.map +1 -1
- package/dest/key_store/local_key_store.d.ts +10 -5
- package/dest/key_store/local_key_store.d.ts.map +1 -1
- package/dest/key_store/local_key_store.js +9 -5
- package/dest/key_store/node_keystore_adapter.d.ts +18 -5
- package/dest/key_store/node_keystore_adapter.d.ts.map +1 -1
- package/dest/key_store/node_keystore_adapter.js +18 -4
- package/dest/key_store/web3signer_key_store.d.ts +10 -5
- package/dest/key_store/web3signer_key_store.d.ts.map +1 -1
- package/dest/key_store/web3signer_key_store.js +9 -5
- package/dest/metrics.d.ts +16 -3
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +58 -30
- package/dest/proposal_handler.d.ts +108 -0
- package/dest/proposal_handler.d.ts.map +1 -0
- package/dest/proposal_handler.js +974 -0
- package/dest/validator.d.ts +74 -23
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +328 -65
- package/package.json +23 -13
- package/src/checkpoint_builder.ts +417 -0
- package/src/config.ts +43 -12
- package/src/duties/validation_service.ts +162 -40
- package/src/factory.ts +31 -11
- package/src/index.ts +2 -1
- package/src/key_store/ha_key_store.ts +269 -0
- package/src/key_store/index.ts +1 -0
- package/src/key_store/interface.ts +44 -5
- package/src/key_store/local_key_store.ts +14 -5
- package/src/key_store/node_keystore_adapter.ts +28 -5
- package/src/key_store/web3signer_key_store.ts +18 -5
- package/src/metrics.ts +81 -33
- package/src/proposal_handler.ts +1042 -0
- package/src/validator.ts +514 -100
- package/dest/block_proposal_handler.d.ts +0 -52
- package/dest/block_proposal_handler.d.ts.map +0 -1
- package/dest/block_proposal_handler.js +0 -290
- package/src/block_proposal_handler.ts +0 -341
package/dest/validator.js
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
|
+
import { getBlobsPerL1Block } from '@aztec/blob-lib';
|
|
2
|
+
import { CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
1
3
|
import { createLogger } from '@aztec/foundation/log';
|
|
2
4
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
3
5
|
import { sleep } from '@aztec/foundation/sleep';
|
|
4
6
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
5
7
|
import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
|
|
6
8
|
import { OffenseType, WANT_TO_SLASH_EVENT } from '@aztec/slasher';
|
|
9
|
+
import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
7
10
|
import { AttestationTimeoutError } from '@aztec/stdlib/validators';
|
|
8
11
|
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
12
|
+
import { createHASigner, createLocalSignerWithProtection, createSignerFromSharedDb } from '@aztec/validator-ha-signer/factory';
|
|
13
|
+
import { DutyType } from '@aztec/validator-ha-signer/types';
|
|
9
14
|
import { EventEmitter } from 'events';
|
|
10
|
-
import { BlockProposalHandler } from './block_proposal_handler.js';
|
|
11
15
|
import { ValidationService } from './duties/validation_service.js';
|
|
16
|
+
import { HAKeyStore } from './key_store/ha_key_store.js';
|
|
12
17
|
import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
|
|
13
18
|
import { ValidatorMetrics } from './metrics.js';
|
|
19
|
+
import { ProposalHandler } from './proposal_handler.js';
|
|
14
20
|
// We maintain a set of proposers who have proposed invalid blocks.
|
|
15
21
|
// Just cap the set to avoid unbounded growth.
|
|
16
22
|
const MAX_PROPOSERS_OF_INVALID_BLOCKS = 1000;
|
|
@@ -25,8 +31,14 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
25
31
|
keyStore;
|
|
26
32
|
epochCache;
|
|
27
33
|
p2pClient;
|
|
28
|
-
|
|
34
|
+
proposalHandler;
|
|
35
|
+
blockSource;
|
|
36
|
+
checkpointsBuilder;
|
|
37
|
+
worldState;
|
|
38
|
+
l1ToL2MessageSource;
|
|
29
39
|
config;
|
|
40
|
+
blobClient;
|
|
41
|
+
slashingProtectionSigner;
|
|
30
42
|
dateProvider;
|
|
31
43
|
tracer;
|
|
32
44
|
validationService;
|
|
@@ -34,18 +46,20 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
34
46
|
log;
|
|
35
47
|
// Whether it has already registered handlers on the p2p client
|
|
36
48
|
hasRegisteredHandlers;
|
|
37
|
-
|
|
38
|
-
|
|
49
|
+
/** Tracks the last block proposal we created, to detect duplicate proposal attempts. */ lastProposedBlock;
|
|
50
|
+
/** Tracks the last checkpoint proposal we created. */ lastProposedCheckpoint;
|
|
39
51
|
lastEpochForCommitteeUpdateLoop;
|
|
40
52
|
epochCacheUpdateLoop;
|
|
53
|
+
/** Tracks the last epoch in which each attester successfully submitted at least one attestation. */ lastAttestedEpochByAttester;
|
|
41
54
|
proposersOfInvalidBlocks;
|
|
42
|
-
|
|
43
|
-
|
|
55
|
+
/** Tracks the last checkpoint proposal we attested to, to prevent equivocation. */ lastAttestedProposal;
|
|
56
|
+
constructor(keyStore, epochCache, p2pClient, proposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, slashingProtectionSigner, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
|
|
57
|
+
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 = new Set();
|
|
44
58
|
// Create child logger with fisherman prefix if in fisherman mode
|
|
45
59
|
this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
|
|
46
60
|
this.tracer = telemetry.getTracer('Validator');
|
|
47
61
|
this.metrics = new ValidatorMetrics(telemetry);
|
|
48
|
-
this.validationService = new ValidationService(keyStore, this.log.createChild('validation-service'));
|
|
62
|
+
this.validationService = new ValidationService(keyStore, this.getSignatureContext(), this.log.createChild('validation-service'));
|
|
49
63
|
// Refresh epoch cache every second to trigger alert if participation in committee changes
|
|
50
64
|
this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), this.log, 1000);
|
|
51
65
|
const myAddresses = this.getValidatorAddresses();
|
|
@@ -79,6 +93,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
79
93
|
this.log.trace(`No committee found for slot`);
|
|
80
94
|
return;
|
|
81
95
|
}
|
|
96
|
+
this.metrics.setCurrentEpoch(epoch);
|
|
82
97
|
if (epoch !== this.lastEpochForCommitteeUpdateLoop) {
|
|
83
98
|
const me = this.getValidatorAddresses();
|
|
84
99
|
const committeeSet = new Set(committee.map((v)=>v.toString()));
|
|
@@ -94,27 +109,62 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
94
109
|
this.log.error(`Error updating epoch committee`, err);
|
|
95
110
|
}
|
|
96
111
|
}
|
|
97
|
-
static new(config,
|
|
112
|
+
static async new(config, checkpointsBuilder, worldState, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), slashingProtectionDb) {
|
|
98
113
|
const metrics = new ValidatorMetrics(telemetry);
|
|
99
114
|
const blockProposalValidator = new BlockProposalValidator(epochCache, {
|
|
100
|
-
txsPermitted: !config.disableTransactions
|
|
115
|
+
txsPermitted: !config.disableTransactions,
|
|
116
|
+
maxTxsPerBlock: config.validateMaxTxsPerBlock,
|
|
117
|
+
signatureContext: {
|
|
118
|
+
chainId: config.l1ChainId,
|
|
119
|
+
rollupAddress: config.l1Contracts.rollupAddress
|
|
120
|
+
}
|
|
101
121
|
});
|
|
102
|
-
const
|
|
103
|
-
const
|
|
122
|
+
const proposalHandler = new ProposalHandler(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, epochCache, config, blobClient, metrics, dateProvider, telemetry, undefined);
|
|
123
|
+
const nodeKeystoreAdapter = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
|
|
124
|
+
let slashingProtectionSigner;
|
|
125
|
+
if (slashingProtectionDb) {
|
|
126
|
+
// Shared database mode: use a pre-existing database (e.g. for testing HA setups).
|
|
127
|
+
({ signer: slashingProtectionSigner } = createSignerFromSharedDb(slashingProtectionDb, config, {
|
|
128
|
+
telemetryClient: telemetry,
|
|
129
|
+
dateProvider
|
|
130
|
+
}));
|
|
131
|
+
} else if (config.haSigningEnabled) {
|
|
132
|
+
// Multi-node HA mode: use PostgreSQL-backed distributed locking.
|
|
133
|
+
// If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
|
|
134
|
+
const haConfig = {
|
|
135
|
+
...config,
|
|
136
|
+
maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000
|
|
137
|
+
};
|
|
138
|
+
({ signer: slashingProtectionSigner } = await createHASigner(haConfig, {
|
|
139
|
+
telemetryClient: telemetry,
|
|
140
|
+
dateProvider
|
|
141
|
+
}));
|
|
142
|
+
} else {
|
|
143
|
+
// Single-node mode: use LMDB-backed local signing protection.
|
|
144
|
+
// This prevents double-signing if the node crashes and restarts mid-proposal.
|
|
145
|
+
({ signer: slashingProtectionSigner } = await createLocalSignerWithProtection(config, {
|
|
146
|
+
telemetryClient: telemetry,
|
|
147
|
+
dateProvider
|
|
148
|
+
}));
|
|
149
|
+
}
|
|
150
|
+
const validatorKeyStore = new HAKeyStore(nodeKeystoreAdapter, slashingProtectionSigner);
|
|
151
|
+
const validator = new ValidatorClient(validatorKeyStore, epochCache, p2pClient, proposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, slashingProtectionSigner, dateProvider, telemetry);
|
|
104
152
|
return validator;
|
|
105
153
|
}
|
|
106
154
|
getValidatorAddresses() {
|
|
107
155
|
return this.keyStore.getAddresses().filter((addr)=>!this.config.disabledValidators.some((disabled)=>disabled.equals(addr)));
|
|
108
156
|
}
|
|
109
|
-
|
|
110
|
-
return this.
|
|
157
|
+
getProposalHandler() {
|
|
158
|
+
return this.proposalHandler;
|
|
111
159
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
|
|
160
|
+
signWithAddress(addr, msg, context) {
|
|
161
|
+
return this.keyStore.signTypedDataWithAddress(addr, msg, context);
|
|
115
162
|
}
|
|
116
|
-
|
|
117
|
-
return
|
|
163
|
+
getSignatureContext() {
|
|
164
|
+
return {
|
|
165
|
+
chainId: this.config.l1ChainId,
|
|
166
|
+
rollupAddress: this.config.l1Contracts.rollupAddress
|
|
167
|
+
};
|
|
118
168
|
}
|
|
119
169
|
getCoinbaseForAttestor(attestor) {
|
|
120
170
|
return this.keyStore.getCoinbaseAddress(attestor);
|
|
@@ -131,11 +181,17 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
131
181
|
...config
|
|
132
182
|
};
|
|
133
183
|
}
|
|
184
|
+
reloadKeystore(newManager) {
|
|
185
|
+
const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
|
|
186
|
+
this.keyStore = new HAKeyStore(newAdapter, this.slashingProtectionSigner);
|
|
187
|
+
this.validationService = new ValidationService(this.keyStore, this.getSignatureContext(), this.log.createChild('validation-service'));
|
|
188
|
+
}
|
|
134
189
|
async start() {
|
|
135
190
|
if (this.epochCacheUpdateLoop.isRunning()) {
|
|
136
191
|
this.log.warn(`Validator client already started`);
|
|
137
192
|
return;
|
|
138
193
|
}
|
|
194
|
+
await this.keyStore.start();
|
|
139
195
|
await this.registerHandlers();
|
|
140
196
|
const myAddresses = this.getValidatorAddresses();
|
|
141
197
|
const inCommittee = await this.epochCache.filterInCommittee('now', myAddresses);
|
|
@@ -148,47 +204,75 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
148
204
|
}
|
|
149
205
|
async stop() {
|
|
150
206
|
await this.epochCacheUpdateLoop.stop();
|
|
207
|
+
await this.keyStore.stop();
|
|
151
208
|
}
|
|
152
209
|
/** Register handlers on the p2p client */ async registerHandlers() {
|
|
153
210
|
if (!this.hasRegisteredHandlers) {
|
|
154
211
|
this.hasRegisteredHandlers = true;
|
|
155
212
|
this.log.debug(`Registering validator handlers for p2p client`);
|
|
156
|
-
|
|
157
|
-
this.
|
|
213
|
+
// Block proposal handler - validates but does NOT attest (validators only attest to checkpoints)
|
|
214
|
+
const blockHandler = (block, proposalSender)=>this.validateBlockProposal(block, proposalSender);
|
|
215
|
+
this.p2pClient.registerBlockProposalHandler(blockHandler);
|
|
216
|
+
// Checkpoint proposal handler - validates and creates attestations
|
|
217
|
+
// The checkpoint is received as CheckpointProposalCore since the lastBlock is extracted
|
|
218
|
+
// and processed separately via the block handler above.
|
|
219
|
+
const checkpointHandler = (checkpoint, proposalSender)=>this.attestToCheckpointProposal(checkpoint, proposalSender);
|
|
220
|
+
this.p2pClient.registerValidatorCheckpointProposalHandler(checkpointHandler);
|
|
221
|
+
// Duplicate proposal handler - triggers slashing for equivocation
|
|
222
|
+
this.p2pClient.registerDuplicateProposalCallback((info)=>{
|
|
223
|
+
this.handleDuplicateProposal(info);
|
|
224
|
+
});
|
|
225
|
+
// Duplicate attestation handler - triggers slashing for attestation equivocation
|
|
226
|
+
this.p2pClient.registerDuplicateAttestationCallback((info)=>{
|
|
227
|
+
this.handleDuplicateAttestation(info);
|
|
228
|
+
});
|
|
158
229
|
const myAddresses = this.getValidatorAddresses();
|
|
159
230
|
this.p2pClient.registerThisValidatorAddresses(myAddresses);
|
|
160
231
|
await this.p2pClient.addReqRespSubProtocol(ReqRespSubProtocol.AUTH, this.handleAuthRequest.bind(this));
|
|
161
232
|
}
|
|
162
233
|
}
|
|
163
|
-
|
|
234
|
+
/**
|
|
235
|
+
* Validate a block proposal from a peer.
|
|
236
|
+
* Note: Validators do NOT attest to individual blocks - attestations are only for checkpoint proposals.
|
|
237
|
+
* @returns true if the proposal is valid, false otherwise
|
|
238
|
+
*/ async validateBlockProposal(proposal, proposalSender) {
|
|
164
239
|
const slotNumber = proposal.slotNumber;
|
|
240
|
+
// Note: During escape hatch, we still want to "validate" proposals for observability,
|
|
241
|
+
// but we intentionally reject them and disable slashing invalid block and attestation flow.
|
|
242
|
+
const escapeHatchOpen = await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber);
|
|
165
243
|
const proposer = proposal.getSender();
|
|
166
244
|
// Reject proposals with invalid signatures
|
|
167
245
|
if (!proposer) {
|
|
168
|
-
this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
|
|
169
|
-
return
|
|
246
|
+
this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
|
|
247
|
+
return false;
|
|
170
248
|
}
|
|
171
|
-
//
|
|
249
|
+
// Log self-proposals from HA peers (same validator key on different nodes)
|
|
250
|
+
if (this.getValidatorAddresses().some((addr)=>addr.equals(proposer))) {
|
|
251
|
+
this.log.verbose(`Processing block proposal from HA peer for slot ${slotNumber}`, {
|
|
252
|
+
proposer: proposer.toString(),
|
|
253
|
+
slotNumber
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
// Check if we're in the committee (for metrics purposes)
|
|
172
257
|
const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
|
|
173
258
|
const partOfCommittee = inCommittee.length > 0;
|
|
174
259
|
const proposalInfo = {
|
|
175
260
|
...proposal.toBlockInfo(),
|
|
176
261
|
proposer: proposer.toString()
|
|
177
262
|
};
|
|
178
|
-
this.log.info(`Received proposal for slot ${slotNumber}`, {
|
|
263
|
+
this.log.info(`Received block proposal for slot ${slotNumber}`, {
|
|
179
264
|
...proposalInfo,
|
|
180
265
|
txHashes: proposal.txHashes.map((t)=>t.toString()),
|
|
181
266
|
fishermanMode: this.config.fishermanMode || false
|
|
182
267
|
});
|
|
183
|
-
// Reexecute txs if we are part of the committee
|
|
184
|
-
// invalid proposals even when not in the committee, or if we are configured to always reexecute for monitoring purposes.
|
|
268
|
+
// Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
|
|
185
269
|
// In fisherman mode, we always reexecute to validate proposals.
|
|
186
|
-
const {
|
|
187
|
-
const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n
|
|
188
|
-
const validationResult = await this.
|
|
270
|
+
const { slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } = this.config;
|
|
271
|
+
const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n || partOfCommittee || alwaysReexecuteBlockProposals || this.blobClient.canUpload();
|
|
272
|
+
const validationResult = await this.proposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute && !escapeHatchOpen);
|
|
189
273
|
if (!validationResult.isValid) {
|
|
190
|
-
this.log.warn(`Proposal validation failed: ${validationResult.reason}`, proposalInfo);
|
|
191
274
|
const reason = validationResult.reason || 'unknown';
|
|
275
|
+
this.log.warn(`Block proposal validation failed: ${reason}`, proposalInfo);
|
|
192
276
|
// Classify failure reason: bad proposal vs node issue
|
|
193
277
|
const badProposalReasons = [
|
|
194
278
|
'invalid_proposal',
|
|
@@ -200,16 +284,75 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
200
284
|
if (badProposalReasons.includes(reason)) {
|
|
201
285
|
this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
|
|
202
286
|
} else {
|
|
203
|
-
// Node issues so we can't
|
|
287
|
+
// Node issues so we can't validate
|
|
204
288
|
this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
|
|
205
289
|
}
|
|
206
290
|
// Slash invalid block proposals (can happen even when not in committee)
|
|
207
|
-
if (validationResult.reason && SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) && slashBroadcastedInvalidBlockPenalty > 0n) {
|
|
291
|
+
if (!escapeHatchOpen && validationResult.reason && SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) && slashBroadcastedInvalidBlockPenalty > 0n) {
|
|
208
292
|
this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
|
|
209
293
|
this.slashInvalidBlock(proposal);
|
|
210
294
|
}
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
this.log.info(`Validated block proposal for slot ${slotNumber}`, {
|
|
298
|
+
...proposalInfo,
|
|
299
|
+
inCommittee: partOfCommittee,
|
|
300
|
+
fishermanMode: this.config.fishermanMode || false,
|
|
301
|
+
escapeHatchOpen
|
|
302
|
+
});
|
|
303
|
+
if (escapeHatchOpen) {
|
|
304
|
+
this.log.warn(`Escape hatch open for slot ${slotNumber}, rejecting block proposal`, proposalInfo);
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Validate and attest to a checkpoint proposal from a peer.
|
|
311
|
+
* The proposal is received as CheckpointProposalCore (without lastBlock) since
|
|
312
|
+
* the lastBlock is extracted and processed separately via the block handler.
|
|
313
|
+
* @returns Checkpoint attestations if valid, undefined otherwise
|
|
314
|
+
*/ async attestToCheckpointProposal(proposal, _proposalSender) {
|
|
315
|
+
const proposalSlotNumber = proposal.slotNumber;
|
|
316
|
+
const proposer = proposal.getSender();
|
|
317
|
+
// If escape hatch is open for this slot's epoch, do not attest.
|
|
318
|
+
if (await this.epochCache.isEscapeHatchOpenAtSlot(proposalSlotNumber)) {
|
|
319
|
+
this.log.warn(`Escape hatch open for slot ${proposalSlotNumber}, skipping checkpoint attestation handling`);
|
|
211
320
|
return undefined;
|
|
212
321
|
}
|
|
322
|
+
// Ignore proposals from ourselves (may happen in HA setups)
|
|
323
|
+
if (proposer && this.getValidatorAddresses().some((addr)=>addr.equals(proposer))) {
|
|
324
|
+
this.log.debug(`Ignoring block proposal from self for slot ${proposalSlotNumber}`, {
|
|
325
|
+
proposer: proposer.toString(),
|
|
326
|
+
proposalSlotNumber
|
|
327
|
+
});
|
|
328
|
+
return undefined;
|
|
329
|
+
}
|
|
330
|
+
// Check that I have any address in the committee where this checkpoint will land before attesting
|
|
331
|
+
const inCommittee = await this.epochCache.filterInCommittee(proposalSlotNumber, this.getValidatorAddresses());
|
|
332
|
+
const partOfCommittee = inCommittee.length > 0;
|
|
333
|
+
const proposalInfo = {
|
|
334
|
+
proposalSlotNumber,
|
|
335
|
+
archive: proposal.archive.toString(),
|
|
336
|
+
proposer: proposer?.toString()
|
|
337
|
+
};
|
|
338
|
+
this.log.info(`Received checkpoint proposal for slot ${proposalSlotNumber}`, {
|
|
339
|
+
...proposalInfo,
|
|
340
|
+
fishermanMode: this.config.fishermanMode || false
|
|
341
|
+
});
|
|
342
|
+
// Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set).
|
|
343
|
+
// Uses the cached result from the all-nodes callback if available (avoids double validation).
|
|
344
|
+
let checkpointNumber;
|
|
345
|
+
if (this.config.skipCheckpointProposalValidation) {
|
|
346
|
+
this.log.warn(`Skipping checkpoint proposal validation for slot ${proposalSlotNumber}`, proposalInfo);
|
|
347
|
+
checkpointNumber = CheckpointNumber(0);
|
|
348
|
+
} else {
|
|
349
|
+
const validationResult = await this.proposalHandler.handleCheckpointProposal(proposal, proposalInfo);
|
|
350
|
+
if (!validationResult.isValid) {
|
|
351
|
+
this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
|
|
352
|
+
return undefined;
|
|
353
|
+
}
|
|
354
|
+
checkpointNumber = validationResult.checkpointNumber;
|
|
355
|
+
}
|
|
213
356
|
// Check that I have any address in current committee before attesting
|
|
214
357
|
// In fisherman mode, we still create attestations for validation even if not in committee
|
|
215
358
|
if (!partOfCommittee && !this.config.fishermanMode) {
|
|
@@ -217,13 +360,22 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
217
360
|
return undefined;
|
|
218
361
|
}
|
|
219
362
|
// Provided all of the above checks pass, we can attest to the proposal
|
|
220
|
-
this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} proposal for slot ${
|
|
363
|
+
this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${proposalSlotNumber}`, {
|
|
221
364
|
...proposalInfo,
|
|
222
365
|
inCommittee: partOfCommittee,
|
|
223
366
|
fishermanMode: this.config.fishermanMode || false
|
|
224
367
|
});
|
|
225
368
|
this.metrics.incSuccessfulAttestations(inCommittee.length);
|
|
226
|
-
//
|
|
369
|
+
// Track epoch participation per attester: count each (attester, epoch) pair at most once
|
|
370
|
+
const proposalEpoch = getEpochAtSlot(proposalSlotNumber, this.epochCache.getL1Constants());
|
|
371
|
+
for (const attester of inCommittee){
|
|
372
|
+
const key = attester.toString();
|
|
373
|
+
const lastEpoch = this.lastAttestedEpochByAttester.get(key);
|
|
374
|
+
if (lastEpoch === undefined || proposalEpoch > lastEpoch) {
|
|
375
|
+
this.lastAttestedEpochByAttester.set(key, proposalEpoch);
|
|
376
|
+
this.metrics.incAttestedEpochCount(attester);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
227
379
|
// Determine which validators should attest
|
|
228
380
|
let attestors;
|
|
229
381
|
if (partOfCommittee) {
|
|
@@ -240,13 +392,64 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
240
392
|
}
|
|
241
393
|
if (this.config.fishermanMode) {
|
|
242
394
|
// bail out early and don't save attestations to the pool in fisherman mode
|
|
243
|
-
this.log.info(`Creating attestations for
|
|
395
|
+
this.log.info(`Creating checkpoint attestations for slot ${proposalSlotNumber}`, {
|
|
244
396
|
...proposalInfo,
|
|
245
397
|
attestors: attestors.map((a)=>a.toString())
|
|
246
398
|
});
|
|
247
399
|
return undefined;
|
|
248
400
|
}
|
|
249
|
-
return this.
|
|
401
|
+
return await this.createCheckpointAttestationsFromProposal(proposal, attestors, checkpointNumber);
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Checks if we should attest to a slot based on equivocation prevention rules.
|
|
405
|
+
* @returns true if we should attest, false if we should skip
|
|
406
|
+
*/ shouldAttestToSlot(slotNumber) {
|
|
407
|
+
// If attestToEquivocatedProposals is true, always allow
|
|
408
|
+
if (this.config.attestToEquivocatedProposals) {
|
|
409
|
+
return true;
|
|
410
|
+
}
|
|
411
|
+
// Check if incoming slot is strictly greater than last attested
|
|
412
|
+
if (this.lastAttestedProposal && slotNumber <= this.lastAttestedProposal.slotNumber) {
|
|
413
|
+
this.log.warn(`Refusing to process a proposal for slot ${slotNumber} given we already attested to a proposal for slot ${this.lastAttestedProposal.slotNumber}`);
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
return true;
|
|
417
|
+
}
|
|
418
|
+
async createCheckpointAttestationsFromProposal(proposal, attestors = [], checkpointNumber) {
|
|
419
|
+
// Equivocation check: must happen right before signing to minimize the race window
|
|
420
|
+
if (!this.shouldAttestToSlot(proposal.slotNumber)) {
|
|
421
|
+
return undefined;
|
|
422
|
+
}
|
|
423
|
+
const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors, checkpointNumber);
|
|
424
|
+
// Track the proposal we attested to (to prevent equivocation)
|
|
425
|
+
this.lastAttestedProposal = proposal;
|
|
426
|
+
await this.p2pClient.addOwnCheckpointAttestations(attestations);
|
|
427
|
+
return attestations;
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Uploads blobs for a checkpoint to the filestore (fire and forget).
|
|
431
|
+
*/ async uploadBlobsForCheckpoint(proposal, proposalInfo) {
|
|
432
|
+
try {
|
|
433
|
+
const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
|
|
434
|
+
if (!lastBlockHeader) {
|
|
435
|
+
this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
|
|
439
|
+
if (blocks.length === 0) {
|
|
440
|
+
this.log.warn(`No blocks found for blob upload`, proposalInfo);
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
const blobFields = blocks.flatMap((b)=>b.toBlobFields());
|
|
444
|
+
const blobs = await getBlobsPerL1Block(blobFields);
|
|
445
|
+
await this.blobClient.sendBlobsToFilestore(blobs);
|
|
446
|
+
this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
|
|
447
|
+
...proposalInfo,
|
|
448
|
+
numBlobs: blobs.length
|
|
449
|
+
});
|
|
450
|
+
} catch (err) {
|
|
451
|
+
this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
|
|
452
|
+
}
|
|
250
453
|
}
|
|
251
454
|
slashInvalidBlock(proposal) {
|
|
252
455
|
const proposer = proposal.getSender();
|
|
@@ -270,59 +473,120 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
270
473
|
}
|
|
271
474
|
]);
|
|
272
475
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
476
|
+
/**
|
|
477
|
+
* Handle detection of a duplicate proposal (equivocation).
|
|
478
|
+
* Emits a slash event when a proposer sends multiple proposals for the same position.
|
|
479
|
+
*/ handleDuplicateProposal(info) {
|
|
480
|
+
const { slot, proposer, type } = info;
|
|
481
|
+
this.log.warn(`Triggering slash event for duplicate ${type} proposal from ${proposer.toString()} at slot ${slot}`, {
|
|
482
|
+
proposer: proposer.toString(),
|
|
483
|
+
slot,
|
|
484
|
+
type
|
|
485
|
+
});
|
|
486
|
+
// Emit slash event
|
|
487
|
+
this.emit(WANT_TO_SLASH_EVENT, [
|
|
488
|
+
{
|
|
489
|
+
validator: proposer,
|
|
490
|
+
amount: this.config.slashDuplicateProposalPenalty,
|
|
491
|
+
offenseType: OffenseType.DUPLICATE_PROPOSAL,
|
|
492
|
+
epochOrSlot: BigInt(slot)
|
|
493
|
+
}
|
|
494
|
+
]);
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Handle detection of a duplicate attestation (equivocation).
|
|
498
|
+
* Emits a slash event when an attester signs attestations for different proposals at the same slot.
|
|
499
|
+
*/ handleDuplicateAttestation(info) {
|
|
500
|
+
const { slot, attester } = info;
|
|
501
|
+
this.log.warn(`Triggering slash event for duplicate attestation from ${attester.toString()} at slot ${slot}`, {
|
|
502
|
+
attester: attester.toString(),
|
|
503
|
+
slot
|
|
504
|
+
});
|
|
505
|
+
this.emit(WANT_TO_SLASH_EVENT, [
|
|
506
|
+
{
|
|
507
|
+
validator: attester,
|
|
508
|
+
amount: this.config.slashDuplicateAttestationPenalty,
|
|
509
|
+
offenseType: OffenseType.DUPLICATE_ATTESTATION,
|
|
510
|
+
epochOrSlot: BigInt(slot)
|
|
511
|
+
}
|
|
512
|
+
]);
|
|
513
|
+
}
|
|
514
|
+
async createBlockProposal(blockHeader, checkpointNumber, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, options = {}) {
|
|
515
|
+
// Validate that we're not creating a proposal for an older or equal position
|
|
516
|
+
if (this.lastProposedBlock) {
|
|
517
|
+
const lastSlot = this.lastProposedBlock.slotNumber;
|
|
518
|
+
const lastIndex = this.lastProposedBlock.indexWithinCheckpoint;
|
|
519
|
+
const newSlot = blockHeader.globalVariables.slotNumber;
|
|
520
|
+
if (newSlot < lastSlot || newSlot === lastSlot && indexWithinCheckpoint <= lastIndex) {
|
|
521
|
+
throw new Error(`Cannot create block proposal for slot ${newSlot} index ${indexWithinCheckpoint}: ` + `already proposed block for slot ${lastSlot} index ${lastIndex}`);
|
|
522
|
+
}
|
|
277
523
|
}
|
|
278
|
-
|
|
524
|
+
this.log.info(`Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`);
|
|
525
|
+
const newProposal = await this.validationService.createBlockProposal(blockHeader, checkpointNumber, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, {
|
|
279
526
|
...options,
|
|
280
527
|
broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
|
|
281
528
|
});
|
|
282
|
-
this.
|
|
529
|
+
this.lastProposedBlock = newProposal;
|
|
530
|
+
return newProposal;
|
|
531
|
+
}
|
|
532
|
+
async createCheckpointProposal(checkpointHeader, archive, checkpointNumber, feeAssetPriceModifier, lastBlockProposal, proposerAddress, options = {}) {
|
|
533
|
+
// Validate that we're not creating a proposal for an older or equal slot
|
|
534
|
+
if (this.lastProposedCheckpoint) {
|
|
535
|
+
const lastSlot = this.lastProposedCheckpoint.slotNumber;
|
|
536
|
+
const newSlot = checkpointHeader.slotNumber;
|
|
537
|
+
if (newSlot <= lastSlot) {
|
|
538
|
+
throw new Error(`Cannot create checkpoint proposal for slot ${newSlot}: ` + `already proposed checkpoint for slot ${lastSlot}`);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
|
|
542
|
+
const newProposal = await this.validationService.createCheckpointProposal(checkpointHeader, archive, checkpointNumber, feeAssetPriceModifier, lastBlockProposal, proposerAddress, options);
|
|
543
|
+
this.lastProposedCheckpoint = newProposal;
|
|
283
544
|
return newProposal;
|
|
284
545
|
}
|
|
285
546
|
async broadcastBlockProposal(proposal) {
|
|
286
547
|
await this.p2pClient.broadcastProposal(proposal);
|
|
287
548
|
}
|
|
288
|
-
async signAttestationsAndSigners(attestationsAndSigners, proposer) {
|
|
289
|
-
return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
|
|
549
|
+
async signAttestationsAndSigners(attestationsAndSigners, proposer, slot, checkpointNumber) {
|
|
550
|
+
return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, checkpointNumber);
|
|
290
551
|
}
|
|
291
|
-
async collectOwnAttestations(proposal) {
|
|
292
|
-
const slot = proposal.
|
|
552
|
+
async collectOwnAttestations(proposal, checkpointNumber) {
|
|
553
|
+
const slot = proposal.slotNumber;
|
|
293
554
|
const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
|
|
294
555
|
this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, {
|
|
295
556
|
inCommittee
|
|
296
557
|
});
|
|
297
|
-
const attestations = await this.
|
|
558
|
+
const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee, checkpointNumber);
|
|
559
|
+
if (!attestations) {
|
|
560
|
+
return [];
|
|
561
|
+
}
|
|
298
562
|
// We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
|
|
299
563
|
// other nodes can see that our validators did attest to this block proposal, and do not slash us
|
|
300
564
|
// due to inactivity for missed attestations.
|
|
301
|
-
void this.p2pClient.
|
|
565
|
+
void this.p2pClient.broadcastCheckpointAttestations(attestations).catch((err)=>{
|
|
302
566
|
this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
|
|
303
567
|
});
|
|
304
568
|
return attestations;
|
|
305
569
|
}
|
|
306
|
-
async collectAttestations(proposal, required, deadline) {
|
|
307
|
-
// Wait and poll the p2pClient's attestation pool for this
|
|
308
|
-
const slot = proposal.
|
|
570
|
+
async collectAttestations(proposal, required, deadline, checkpointNumber) {
|
|
571
|
+
// Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
|
|
572
|
+
const slot = proposal.slotNumber;
|
|
309
573
|
this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
|
|
310
574
|
if (+deadline < this.dateProvider.now()) {
|
|
311
575
|
this.log.error(`Deadline ${deadline.toISOString()} for collecting ${required} attestations for slot ${slot} is in the past`);
|
|
312
576
|
throw new AttestationTimeoutError(0, required, slot);
|
|
313
577
|
}
|
|
314
|
-
await this.collectOwnAttestations(proposal);
|
|
578
|
+
await this.collectOwnAttestations(proposal, checkpointNumber);
|
|
315
579
|
const proposalId = proposal.archive.toString();
|
|
316
580
|
const myAddresses = this.getValidatorAddresses();
|
|
317
581
|
let attestations = [];
|
|
318
582
|
while(true){
|
|
319
|
-
// Filter out attestations with a mismatching
|
|
583
|
+
// Filter out attestations with a mismatching archive. This should NOT happen since we have verified
|
|
320
584
|
// the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
|
|
321
|
-
const collectedAttestations = (await this.p2pClient.
|
|
322
|
-
if (!attestation.
|
|
323
|
-
this.log.warn(`Received attestation for slot ${slot} with mismatched
|
|
324
|
-
|
|
325
|
-
|
|
585
|
+
const collectedAttestations = (await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalId)).filter((attestation)=>{
|
|
586
|
+
if (!attestation.archive.equals(proposal.archive)) {
|
|
587
|
+
this.log.warn(`Received attestation for slot ${slot} with mismatched archive from ${attestation.getSender()?.toString()}`, {
|
|
588
|
+
attestationArchive: attestation.archive.toString(),
|
|
589
|
+
proposalArchive: proposal.archive.toString()
|
|
326
590
|
});
|
|
327
591
|
return false;
|
|
328
592
|
}
|
|
@@ -354,11 +618,6 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
354
618
|
await sleep(this.config.attestationPollingIntervalMs);
|
|
355
619
|
}
|
|
356
620
|
}
|
|
357
|
-
async createBlockAttestationsFromProposal(proposal, attestors = []) {
|
|
358
|
-
const attestations = await this.validationService.attestToProposal(proposal, attestors);
|
|
359
|
-
await this.p2pClient.addAttestations(attestations);
|
|
360
|
-
return attestations;
|
|
361
|
-
}
|
|
362
621
|
async handleAuthRequest(peer, msg) {
|
|
363
622
|
const authRequest = AuthRequest.fromBuffer(msg);
|
|
364
623
|
const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch((_)=>undefined);
|
|
@@ -373,7 +632,11 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
373
632
|
return Buffer.alloc(0);
|
|
374
633
|
}
|
|
375
634
|
const payloadToSign = authRequest.getPayloadToSign();
|
|
376
|
-
|
|
635
|
+
// AUTH_REQUEST doesn't require HA protection - multiple signatures are safe
|
|
636
|
+
const context = {
|
|
637
|
+
dutyType: DutyType.AUTH_REQUEST
|
|
638
|
+
};
|
|
639
|
+
const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign, context);
|
|
377
640
|
const authResponse = new AuthResponse(statusMessage, signature);
|
|
378
641
|
return authResponse.toBuffer();
|
|
379
642
|
}
|