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