-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathopPool.ts
453 lines (407 loc) Β· 17.2 KB
/
opPool.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import {fromHexString} from "@chainsafe/ssz";
import {
CachedBeaconStateAllForks,
computeEpochAtSlot,
computeStartSlotAtEpoch,
getAttesterSlashableIndices,
isValidVoluntaryExit,
} from "@lodestar/state-transition";
import {Repository, Id} from "@lodestar/db";
import {
MAX_PROPOSER_SLASHINGS,
MAX_VOLUNTARY_EXITS,
MAX_BLS_TO_EXECUTION_CHANGES,
BLS_WITHDRAWAL_PREFIX,
MAX_ATTESTER_SLASHINGS,
ForkSeq,
MAX_ATTESTER_SLASHINGS_ELECTRA,
} from "@lodestar/params";
import {toHex, toRootHex} from "@lodestar/utils";
import {Epoch, phase0, capella, ssz, ValidatorIndex, SignedBeaconBlock, AttesterSlashing} from "@lodestar/types";
import {IBeaconDb} from "../../db/index.js";
import {SignedBLSToExecutionChangeVersioned} from "../../util/types.js";
import {BlockType} from "../interface.js";
import {Metrics} from "../../metrics/metrics.js";
import {BlockProductionStep} from "../produceBlock/produceBlockBody.js";
import {isValidBlsToExecutionChangeForBlockInclusion} from "./utils.js";
type HexRoot = string;
type AttesterSlashingCached = {
attesterSlashing: phase0.AttesterSlashing;
intersectingIndices: number[];
};
export class OpPool {
/** Map of uniqueId(AttesterSlashing) -> AttesterSlashing */
private readonly attesterSlashings = new Map<HexRoot, AttesterSlashingCached>();
/** Map of to slash validator index -> ProposerSlashing */
private readonly proposerSlashings = new Map<ValidatorIndex, phase0.ProposerSlashing>();
/** Map of to exit validator index -> SignedVoluntaryExit */
private readonly voluntaryExits = new Map<ValidatorIndex, phase0.SignedVoluntaryExit>();
/** Set of seen attester slashing indexes. No need to prune */
private readonly attesterSlashingIndexes = new Set<ValidatorIndex>();
/** Map of validator index -> SignedBLSToExecutionChange */
private readonly blsToExecutionChanges = new Map<ValidatorIndex, SignedBLSToExecutionChangeVersioned>();
// Getters for metrics
get attesterSlashingsSize(): number {
return this.attesterSlashings.size;
}
get proposerSlashingsSize(): number {
return this.proposerSlashings.size;
}
get voluntaryExitsSize(): number {
return this.voluntaryExits.size;
}
get blsToExecutionChangeSize(): number {
return this.blsToExecutionChanges.size;
}
async fromPersisted(db: IBeaconDb): Promise<void> {
const [attesterSlashings, proposerSlashings, voluntaryExits, blsToExecutionChanges] = await Promise.all([
db.attesterSlashing.entries(),
db.proposerSlashing.values(),
db.voluntaryExit.values(),
db.blsToExecutionChange.values(),
]);
for (const attesterSlashing of attesterSlashings) {
this.insertAttesterSlashing(attesterSlashing.value, attesterSlashing.key);
}
for (const proposerSlashing of proposerSlashings) {
this.insertProposerSlashing(proposerSlashing);
}
for (const voluntaryExit of voluntaryExits) {
this.insertVoluntaryExit(voluntaryExit);
}
for (const item of blsToExecutionChanges) {
this.insertBlsToExecutionChange(item.data, item.preCapella);
}
}
async toPersisted(db: IBeaconDb): Promise<void> {
await Promise.all([
persistDiff(
db.attesterSlashing,
Array.from(this.attesterSlashings.entries()).map(([key, value]) => ({
key: fromHexString(key),
value: value.attesterSlashing,
})),
toHex
),
persistDiff(
db.proposerSlashing,
Array.from(this.proposerSlashings.entries()).map(([key, value]) => ({key, value})),
(index) => index
),
persistDiff(
db.voluntaryExit,
Array.from(this.voluntaryExits.entries()).map(([key, value]) => ({key, value})),
(index) => index
),
persistDiff(
db.blsToExecutionChange,
Array.from(this.blsToExecutionChanges.entries()).map(([key, value]) => ({key, value})),
(index) => index
),
]);
}
// Use the opPool as seen cache for gossip validation
/** Returns false if at least one intersecting index has not been seen yet */
hasSeenAttesterSlashing(intersectingIndices: ValidatorIndex[]): boolean {
for (const validatorIndex of intersectingIndices) {
if (!this.attesterSlashingIndexes.has(validatorIndex)) {
return false;
}
}
return true;
}
hasSeenVoluntaryExit(validatorIndex: ValidatorIndex): boolean {
return this.voluntaryExits.has(validatorIndex);
}
hasSeenBlsToExecutionChange(validatorIndex: ValidatorIndex): boolean {
return this.blsToExecutionChanges.has(validatorIndex);
}
hasSeenProposerSlashing(validatorIndex: ValidatorIndex): boolean {
return this.proposerSlashings.has(validatorIndex);
}
/** Must be validated beforehand */
insertAttesterSlashing(attesterSlashing: phase0.AttesterSlashing, rootHash?: Uint8Array): void {
if (!rootHash) rootHash = ssz.phase0.AttesterSlashing.hashTreeRoot(attesterSlashing);
// TODO: Do once and cache attached to the AttesterSlashing object
const intersectingIndices = getAttesterSlashableIndices(attesterSlashing);
this.attesterSlashings.set(toRootHex(rootHash), {
attesterSlashing,
intersectingIndices,
});
for (const index of intersectingIndices) {
this.attesterSlashingIndexes.add(index);
}
}
/** Must be validated beforehand */
insertProposerSlashing(proposerSlashing: phase0.ProposerSlashing): void {
this.proposerSlashings.set(proposerSlashing.signedHeader1.message.proposerIndex, proposerSlashing);
}
/** Must be validated beforehand */
insertVoluntaryExit(voluntaryExit: phase0.SignedVoluntaryExit): void {
this.voluntaryExits.set(voluntaryExit.message.validatorIndex, voluntaryExit);
}
/** Must be validated beforehand */
insertBlsToExecutionChange(blsToExecutionChange: capella.SignedBLSToExecutionChange, preCapella = false): void {
this.blsToExecutionChanges.set(blsToExecutionChange.message.validatorIndex, {
data: blsToExecutionChange,
preCapella,
});
}
/**
* Get proposer and attester slashings and voluntary exits and bls to execution change for inclusion in a block.
*
* This function computes both types of slashings and exits, because attester slashings and exits may be invalidated by
* slashings included earlier in the block.
*/
getSlashingsAndExits(
state: CachedBeaconStateAllForks,
blockType: BlockType,
metrics: Metrics | null
): [
AttesterSlashing[],
phase0.ProposerSlashing[],
phase0.SignedVoluntaryExit[],
capella.SignedBLSToExecutionChange[],
] {
const {config} = state;
const stateEpoch = computeEpochAtSlot(state.slot);
const stateFork = config.getForkSeq(state.slot);
const toBeSlashedIndices = new Set<ValidatorIndex>();
const proposerSlashings: phase0.ProposerSlashing[] = [];
const stepsMetrics =
blockType === BlockType.Full
? metrics?.executionBlockProductionTimeSteps
: metrics?.builderBlockProductionTimeSteps;
const endProposerSlashing = stepsMetrics?.startTimer();
for (const proposerSlashing of this.proposerSlashings.values()) {
const index = proposerSlashing.signedHeader1.message.proposerIndex;
const validator = state.validators.getReadonly(index);
if (!validator.slashed && validator.activationEpoch <= stateEpoch && stateEpoch < validator.withdrawableEpoch) {
proposerSlashings.push(proposerSlashing);
// Set of validators to be slashed, so we don't attempt to construct invalid attester slashings.
toBeSlashedIndices.add(index);
if (proposerSlashings.length >= MAX_PROPOSER_SLASHINGS) {
break;
}
}
}
endProposerSlashing?.({
step: BlockProductionStep.proposerSlashing,
});
const endAttesterSlashings = stepsMetrics?.startTimer();
const attesterSlashings: AttesterSlashing[] = [];
const maxAttesterSlashings = stateFork >= ForkSeq.electra ? MAX_ATTESTER_SLASHINGS_ELECTRA : MAX_ATTESTER_SLASHINGS;
attesterSlashing: for (const attesterSlashing of this.attesterSlashings.values()) {
/** Indices slashable in this attester slashing */
const slashableIndices = new Set<ValidatorIndex>();
for (let i = 0; i < attesterSlashing.intersectingIndices.length; i++) {
const index = attesterSlashing.intersectingIndices[i];
// If we already have a slashing for this index, we can continue on to the next slashing
if (toBeSlashedIndices.has(index)) {
continue attesterSlashing;
}
const validator = state.validators.getReadonly(index);
if (isSlashableAtEpoch(validator, stateEpoch)) {
slashableIndices.add(index);
}
if (attesterSlashings.length >= maxAttesterSlashings) {
break attesterSlashing;
}
}
// If there were slashable indices in this slashing
// Then include the slashing and count the slashable indices
if (slashableIndices.size > 0) {
attesterSlashings.push(attesterSlashing.attesterSlashing);
for (const index of slashableIndices) {
toBeSlashedIndices.add(index);
}
}
}
endAttesterSlashings?.({
step: BlockProductionStep.attesterSlashings,
});
const endVoluntaryExits = stepsMetrics?.startTimer();
const voluntaryExits: phase0.SignedVoluntaryExit[] = [];
for (const voluntaryExit of this.voluntaryExits.values()) {
if (
!toBeSlashedIndices.has(voluntaryExit.message.validatorIndex) &&
isValidVoluntaryExit(state, voluntaryExit, false) &&
// Signature validation is skipped in `isValidVoluntaryExit(,,false)` since it was already validated in gossip
// However we must make sure that the signature fork is the same, or it will become invalid if included through
// a future fork.
isVoluntaryExitSignatureIncludable(
stateFork,
config.getForkSeq(computeStartSlotAtEpoch(voluntaryExit.message.epoch))
)
) {
voluntaryExits.push(voluntaryExit);
if (voluntaryExits.length >= MAX_VOLUNTARY_EXITS) {
break;
}
}
}
endVoluntaryExits?.({
step: BlockProductionStep.voluntaryExits,
});
const endBlsToExecutionChanges = stepsMetrics?.startTimer();
const blsToExecutionChanges: capella.SignedBLSToExecutionChange[] = [];
for (const blsToExecutionChange of this.blsToExecutionChanges.values()) {
if (isValidBlsToExecutionChangeForBlockInclusion(state, blsToExecutionChange.data)) {
blsToExecutionChanges.push(blsToExecutionChange.data);
if (blsToExecutionChanges.length >= MAX_BLS_TO_EXECUTION_CHANGES) {
break;
}
}
}
endBlsToExecutionChanges?.({
step: BlockProductionStep.blsToExecutionChanges,
});
return [attesterSlashings, proposerSlashings, voluntaryExits, blsToExecutionChanges];
}
/** For beacon pool API */
// TODO Electra: Update to adapt electra.AttesterSlashing
getAllAttesterSlashings(): phase0.AttesterSlashing[] {
return Array.from(this.attesterSlashings.values()).map((attesterSlashings) => attesterSlashings.attesterSlashing);
}
/** For beacon pool API */
getAllProposerSlashings(): phase0.ProposerSlashing[] {
return Array.from(this.proposerSlashings.values());
}
/** For beacon pool API */
getAllVoluntaryExits(): phase0.SignedVoluntaryExit[] {
return Array.from(this.voluntaryExits.values());
}
/** For beacon pool API */
getAllBlsToExecutionChanges(): SignedBLSToExecutionChangeVersioned[] {
return Array.from(this.blsToExecutionChanges.values());
}
/**
* Prune all types of transactions given the latest head state
*/
pruneAll(headBlock: SignedBeaconBlock, headState: CachedBeaconStateAllForks): void {
this.pruneAttesterSlashings(headState);
this.pruneProposerSlashings(headState);
this.pruneVoluntaryExits(headState);
this.pruneBlsToExecutionChanges(headBlock, headState);
}
/**
* Prune attester slashings for all slashed or withdrawn validators.
*/
private pruneAttesterSlashings(headState: CachedBeaconStateAllForks): void {
const finalizedEpoch = headState.finalizedCheckpoint.epoch;
attesterSlashing: for (const [key, attesterSlashing] of this.attesterSlashings.entries()) {
// Slashings that don't slash any validators can be dropped
for (let i = 0; i < attesterSlashing.intersectingIndices.length; i++) {
const index = attesterSlashing.intersectingIndices[i];
// Declare that a validator is still slashable if they have not exited prior
// to the finalized epoch.
//
// We cannot check the `slashed` field since the `head` is not finalized and
// a fork could un-slash someone.
if (headState.validators.getReadonly(index).exitEpoch > finalizedEpoch) {
continue attesterSlashing;
}
}
// All intersecting indices are not slashable
this.attesterSlashings.delete(key);
}
}
/**
* Prune proposer slashings for validators which are exited in the finalized epoch.
*/
private pruneProposerSlashings(headState: CachedBeaconStateAllForks): void {
const finalizedEpoch = headState.finalizedCheckpoint.epoch;
for (const [key, proposerSlashing] of this.proposerSlashings.entries()) {
const index = proposerSlashing.signedHeader1.message.proposerIndex;
if (headState.validators.getReadonly(index).exitEpoch <= finalizedEpoch) {
this.proposerSlashings.delete(key);
}
}
}
/**
* Call after finalizing
* Prune if validator has already exited at or before the finalized checkpoint of the head.
*/
private pruneVoluntaryExits(headState: CachedBeaconStateAllForks): void {
const {config} = headState;
const headStateFork = config.getForkSeq(headState.slot);
const finalizedEpoch = headState.finalizedCheckpoint.epoch;
for (const [key, voluntaryExit] of this.voluntaryExits.entries()) {
// VoluntaryExit messages signed in the previous fork become invalid and can never be included in any future
// block, so just drop as the head state advances into the next fork.
if (config.getForkSeq(computeStartSlotAtEpoch(voluntaryExit.message.epoch)) < headStateFork) {
this.voluntaryExits.delete(key);
}
// TODO: Improve this simplistic condition
if (voluntaryExit.message.epoch <= finalizedEpoch) {
this.voluntaryExits.delete(key);
}
}
}
/**
* Prune BLS to execution changes that have been applied to the state more than 1 block ago.
* In the worse case where head block is reorged, the same BlsToExecutionChange message can be re-added
* to opPool once gossipsub seen cache TTL passes.
*/
private pruneBlsToExecutionChanges(headBlock: SignedBeaconBlock, headState: CachedBeaconStateAllForks): void {
const {config} = headState;
const recentBlsToExecutionChanges =
config.getForkSeq(headBlock.message.slot) >= ForkSeq.capella
? (headBlock as capella.SignedBeaconBlock).message.body.blsToExecutionChanges
: [];
const recentBlsToExecutionChangeIndexes = new Set(
recentBlsToExecutionChanges.map((blsToExecutionChange) => blsToExecutionChange.message.validatorIndex)
);
for (const [key, blsToExecutionChange] of this.blsToExecutionChanges.entries()) {
const {validatorIndex} = blsToExecutionChange.data.message;
if (!recentBlsToExecutionChangeIndexes.has(validatorIndex)) {
const validator = headState.validators.getReadonly(validatorIndex);
if (validator.withdrawalCredentials[0] !== BLS_WITHDRAWAL_PREFIX) {
this.blsToExecutionChanges.delete(key);
}
}
}
}
}
/**
* Returns true if a pre-validated signature is still valid to be included in a specific block's fork
*/
function isVoluntaryExitSignatureIncludable(stateFork: ForkSeq, voluntaryExitFork: ForkSeq): boolean {
if (stateFork >= ForkSeq.deneb) {
// Exists are perpetually valid https://eips.ethereum.org/EIPS/eip-7044
return true;
} else {
// Can only include exits from the current and previous fork
return voluntaryExitFork === stateFork || voluntaryExitFork === stateFork - 1;
}
}
function isSlashableAtEpoch(validator: phase0.Validator, epoch: Epoch): boolean {
return !validator.slashed && validator.activationEpoch <= epoch && epoch < validator.withdrawableEpoch;
}
/**
* Persist target items `items` in `dbRepo` doing minimum put and delete writes.
* Reads all keys in repository to compute the diff between current persisted data and target data.
*/
async function persistDiff<K extends Id, V>(
dbRepo: Repository<K, V>,
items: {key: K; value: V}[],
serializeKey: (key: K) => number | string
): Promise<void> {
const persistedKeys = await dbRepo.keys();
const itemsToPut: {key: K; value: V}[] = [];
const keysToDelete: K[] = [];
const persistedKeysSerialized = new Set(persistedKeys.map(serializeKey));
for (const item of items) {
if (!persistedKeysSerialized.has(serializeKey(item.key))) {
itemsToPut.push(item);
}
}
const targetKeysSerialized = new Set(items.map((item) => serializeKey(item.key)));
for (const persistedKey of persistedKeys) {
if (!targetKeysSerialized.has(serializeKey(persistedKey))) {
keysToDelete.push(persistedKey);
}
}
if (itemsToPut.length > 0) await dbRepo.batchPut(itemsToPut);
if (keysToDelete.length > 0) await dbRepo.batchDelete(keysToDelete);
}