@lodestar/fork-choice 2.0.0-rc.1 → 2.0.0
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/lib/forkChoice/fastConfirmation/data.d.ts.map +1 -1
- package/lib/forkChoice/fastConfirmation/data.js +3 -2
- package/lib/forkChoice/fastConfirmation/data.js.map +1 -1
- package/lib/forkChoice/fastConfirmation/types.d.ts +30 -4
- package/lib/forkChoice/fastConfirmation/types.d.ts.map +1 -1
- package/lib/forkChoice/fastConfirmation/types.js.map +1 -1
- package/lib/forkChoice/fastConfirmation/utils.d.ts +3 -5
- package/lib/forkChoice/fastConfirmation/utils.d.ts.map +1 -1
- package/lib/forkChoice/fastConfirmation/utils.js +155 -72
- package/lib/forkChoice/fastConfirmation/utils.js.map +1 -1
- package/lib/forkChoice/forkChoice.d.ts +1 -1
- package/lib/forkChoice/forkChoice.d.ts.map +1 -1
- package/lib/forkChoice/forkChoice.js +1 -1
- package/lib/forkChoice/forkChoice.js.map +1 -1
- package/lib/forkChoice/interface.d.ts +2 -0
- package/lib/forkChoice/interface.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/forkChoice/fastConfirmation/data.ts +3 -2
- package/src/forkChoice/fastConfirmation/types.ts +28 -4
- package/src/forkChoice/fastConfirmation/utils.ts +173 -89
- package/src/forkChoice/forkChoice.ts +2 -5
- package/src/forkChoice/interface.ts +2 -0
|
@@ -9,7 +9,13 @@ export type FastConfirmationBalanceSource = {
|
|
|
9
9
|
balances: EffectiveBalanceIncrements;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
export type
|
|
12
|
+
export type BalanceSourceCacheKey = IBeaconStateView | EffectiveBalanceIncrements;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Effective balances of the validators active at the balance source epoch, slashed included, their sum,
|
|
16
|
+
* and the sum over the unslashed ones (`calculate_committee_fraction`)
|
|
17
|
+
*/
|
|
18
|
+
export type ActiveBalances = {balances: EffectiveBalanceIncrements; total: number; unslashedTotal: number};
|
|
13
19
|
|
|
14
20
|
export type ForkChoiceStateGetter = (
|
|
15
21
|
opts: {stateRoot: RootHex; checkpoint?: never} | {stateRoot?: never; checkpoint: CheckpointWithHex}
|
|
@@ -103,16 +109,34 @@ export type FastConfirmationRule = (
|
|
|
103
109
|
|
|
104
110
|
export type BalanceSourceKey = "current" | "previous";
|
|
105
111
|
|
|
112
|
+
/**
|
|
113
|
+
* Committees of the slots ending at a fixed slot, swept backwards one slot at a time over the head
|
|
114
|
+
* state shuffling. A validator is recorded at the latest slot it is assigned to, so the committees of
|
|
115
|
+
* any range ending at that slot are a prefix of `order` with no duplicates, even when the range
|
|
116
|
+
* crosses an epoch boundary and the two epochs' committees overlap.
|
|
117
|
+
*/
|
|
118
|
+
export type CommitteeSweep = {
|
|
119
|
+
/** Marks validators already placed in `order`, indexed by validator index */
|
|
120
|
+
seen: Uint8Array;
|
|
121
|
+
/** Validators of the end slot, then those first seen one slot earlier, and so on */
|
|
122
|
+
order: Uint32Array;
|
|
123
|
+
/** Length of the `order` prefix covering the last `i + 1` slots, for each swept slot `i` */
|
|
124
|
+
lengths: number[];
|
|
125
|
+
/** Committee weight of the last `i + 1` slots, per balance source, filled on demand */
|
|
126
|
+
weightsByKey: Map<BalanceSourceCacheKey, number[]>;
|
|
127
|
+
};
|
|
128
|
+
|
|
106
129
|
// This cache is created once per slot
|
|
107
130
|
export type FastConfirmationCache = {
|
|
108
131
|
blockByRoot: Map<RootHex, ProtoBlock | null>;
|
|
109
132
|
ancestorRoots: Map<string, RootHex[] | null>;
|
|
110
|
-
|
|
133
|
+
committeeSweepByEndSlot: Map<Slot, CommitteeSweep>;
|
|
134
|
+
participantsByRange: Map<string, Uint32Array>;
|
|
111
135
|
isDescendantByRootPair: Map<string, boolean>;
|
|
112
136
|
/** voteRoot -> totalWeight, keyed by sourceKey */
|
|
113
137
|
voteWeightBySource: Map<BalanceSourceKey, Map<RootHex, number>>;
|
|
114
|
-
/**
|
|
115
|
-
|
|
138
|
+
/** active balances and their total, keyed by the balance source's state or balances reference */
|
|
139
|
+
activeBalancesByKey: Map<BalanceSourceCacheKey, ActiveBalances>;
|
|
116
140
|
headState?: IBeaconStateView;
|
|
117
141
|
pulledUpHeadState?: IBeaconStateView;
|
|
118
142
|
checkpointStateByKey: Map<string, IBeaconStateView | null>;
|
|
@@ -3,29 +3,26 @@ import {
|
|
|
3
3
|
EffectiveBalanceIncrements,
|
|
4
4
|
IBeaconStateView,
|
|
5
5
|
computeEpochAtSlot,
|
|
6
|
-
computeSlotsSinceEpochStart,
|
|
7
6
|
computeStartSlotAtEpoch,
|
|
8
7
|
isActiveValidator,
|
|
9
8
|
isStartSlotOfEpoch,
|
|
10
9
|
} from "@lodestar/state-transition";
|
|
11
|
-
import {Epoch, RootHex, Slot
|
|
10
|
+
import {Epoch, RootHex, Slot} from "@lodestar/types";
|
|
12
11
|
import {Logger, fromHex} from "@lodestar/utils";
|
|
13
12
|
import {ExecutionStatus, PayloadStatus, ProtoBlock} from "../../protoArray/interface.js";
|
|
14
13
|
import {CheckpointWithHex, computeTotalBalance, equalCheckpointWithHex} from "../store.js";
|
|
15
14
|
import {
|
|
15
|
+
ActiveBalances,
|
|
16
|
+
type BalanceSourceCacheKey,
|
|
16
17
|
type BalanceSourceKey,
|
|
18
|
+
CommitteeSweep,
|
|
17
19
|
FastConfirmationBalanceSource,
|
|
18
20
|
FastConfirmationCache,
|
|
19
21
|
FastConfirmationContext,
|
|
20
22
|
FastConfirmationSnapshot,
|
|
21
23
|
IFastConfirmationStore,
|
|
22
|
-
type TotalActiveBalanceCacheKey,
|
|
23
24
|
} from "./types.js";
|
|
24
25
|
|
|
25
|
-
// Spec: adjust_committee_weight_estimate_to_ensure_safety
|
|
26
|
-
// https://github.com/ethereum/consensus-specs/blob/master/specs/phase0/fast-confirmation.md#adjust_committee_weight_estimate_to_ensure_safety
|
|
27
|
-
const COMMITTEE_WEIGHT_ESTIMATION_ADJUSTMENT_FACTOR = 5n;
|
|
28
|
-
|
|
29
26
|
const EFFECTIVE_BALANCE_INCREMENT_BIGINT = BigInt(EFFECTIVE_BALANCE_INCREMENT);
|
|
30
27
|
|
|
31
28
|
const SAFETY_THRESHOLD_UNREACHABLE = Object.freeze({
|
|
@@ -182,25 +179,43 @@ export function getCheckpointState(
|
|
|
182
179
|
return state ?? null;
|
|
183
180
|
}
|
|
184
181
|
|
|
185
|
-
|
|
182
|
+
/**
|
|
183
|
+
* Spec: get_slot_committee, for the slots `startSlot..endSlot`.
|
|
184
|
+
* Chain-walk queries ending at `current_slot - 1` share this sweep across many start slots.
|
|
185
|
+
* Extending it backwards avoids rebuilding a union of up to the full validator set per block.
|
|
186
|
+
*/
|
|
187
|
+
function getCommitteeSweep(
|
|
188
|
+
ctx: FastConfirmationContext,
|
|
189
|
+
store: IFastConfirmationStore,
|
|
186
190
|
cache: FastConfirmationCache,
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
):
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
191
|
+
startSlot: Slot,
|
|
192
|
+
endSlot: Slot
|
|
193
|
+
): CommitteeSweep {
|
|
194
|
+
const headState = getHeadState(ctx, store, cache);
|
|
195
|
+
let sweep = cache.committeeSweepByEndSlot.get(endSlot);
|
|
196
|
+
if (sweep === undefined) {
|
|
197
|
+
sweep = {
|
|
198
|
+
seen: new Uint8Array(headState.validatorCount),
|
|
199
|
+
order: new Uint32Array(headState.validatorCount),
|
|
200
|
+
lengths: [],
|
|
201
|
+
weightsByKey: new Map(),
|
|
202
|
+
};
|
|
203
|
+
cache.committeeSweepByEndSlot.set(endSlot, sweep);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
let length = sweep.lengths.at(-1) ?? 0;
|
|
207
|
+
for (let slot = endSlot - sweep.lengths.length; slot >= startSlot; slot--) {
|
|
208
|
+
const committeesCount = headState.getBeaconCommitteeCountPerSlot(computeEpochAtSlot(slot));
|
|
209
|
+
for (let i = 0; i < committeesCount; i++) {
|
|
210
|
+
for (const index of headState.getBeaconCommittee(slot, i)) {
|
|
211
|
+
if (sweep.seen[index] === 1) continue;
|
|
212
|
+
sweep.seen[index] = 1;
|
|
213
|
+
sweep.order[length++] = index;
|
|
214
|
+
}
|
|
200
215
|
}
|
|
216
|
+
sweep.lengths.push(length);
|
|
201
217
|
}
|
|
202
|
-
|
|
203
|
-
return participants;
|
|
218
|
+
return sweep;
|
|
204
219
|
}
|
|
205
220
|
|
|
206
221
|
function getSlotRangeParticipants(
|
|
@@ -209,17 +224,47 @@ function getSlotRangeParticipants(
|
|
|
209
224
|
cache: FastConfirmationCache,
|
|
210
225
|
startSlot: Slot,
|
|
211
226
|
endSlot: Slot
|
|
212
|
-
):
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
227
|
+
): Uint32Array {
|
|
228
|
+
// Empty-slot discounts end at different block slots. A sweep per endpoint would allocate
|
|
229
|
+
// 5 bytes per registered validator for each gap, even when the gap spans just one slot.
|
|
230
|
+
// Cache compact participants instead, shared by support and adversarial-weight calculations.
|
|
231
|
+
if (endSlot !== ctx.getCurrentSlot() - 1) {
|
|
232
|
+
const key = `${startSlot}:${endSlot}`;
|
|
233
|
+
const cached = cache.participantsByRange.get(key);
|
|
234
|
+
if (cached !== undefined) return cached;
|
|
235
|
+
|
|
236
|
+
const state = getHeadState(ctx, store, cache);
|
|
237
|
+
const committees: Uint32Array[] = [];
|
|
238
|
+
let length = 0;
|
|
239
|
+
for (let slot = startSlot; slot <= endSlot; slot++) {
|
|
240
|
+
const count = state.getBeaconCommitteeCountPerSlot(computeEpochAtSlot(slot));
|
|
241
|
+
for (let i = 0; i < count; i++) {
|
|
242
|
+
const committee = state.getBeaconCommittee(slot, i);
|
|
243
|
+
committees.push(committee);
|
|
244
|
+
length += committee.length;
|
|
245
|
+
}
|
|
219
246
|
}
|
|
247
|
+
let participants: Uint32Array;
|
|
248
|
+
// Within an epoch committees partition the active validator set.
|
|
249
|
+
if (computeEpochAtSlot(startSlot) === computeEpochAtSlot(endSlot)) {
|
|
250
|
+
participants = new Uint32Array(length);
|
|
251
|
+
let offset = 0;
|
|
252
|
+
for (const committee of committees) {
|
|
253
|
+
participants.set(committee, offset);
|
|
254
|
+
offset += committee.length;
|
|
255
|
+
}
|
|
256
|
+
} else {
|
|
257
|
+
const unique = new Set<number>();
|
|
258
|
+
for (const committee of committees) {
|
|
259
|
+
for (const index of committee) unique.add(index);
|
|
260
|
+
}
|
|
261
|
+
participants = Uint32Array.from(unique);
|
|
262
|
+
}
|
|
263
|
+
cache.participantsByRange.set(key, participants);
|
|
264
|
+
return participants;
|
|
220
265
|
}
|
|
221
|
-
|
|
222
|
-
return
|
|
266
|
+
const sweep = getCommitteeSweep(ctx, store, cache, startSlot, endSlot);
|
|
267
|
+
return sweep.order.subarray(0, sweep.lengths[endSlot - startSlot]);
|
|
223
268
|
}
|
|
224
269
|
|
|
225
270
|
function isDescendantCached(
|
|
@@ -280,61 +325,94 @@ function getZeroInactiveBalances(balanceSource: FastConfirmationBalanceSource):
|
|
|
280
325
|
return balanceSource.state?.getEffectiveBalanceIncrementsZeroInactive() ?? balanceSource.balances;
|
|
281
326
|
}
|
|
282
327
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
328
|
+
/**
|
|
329
|
+
* Balances read by `get_total_active_balance` and `compute_committee_weight_between_slots`. Unlike the
|
|
330
|
+
* vote scans, these keep slashed validators: the maximum support and the adversarial budget they bound
|
|
331
|
+
* must cover slashed equivocators, whose weight `getEquivocationScore` subtracts. `unslashedTotal` serves
|
|
332
|
+
* `compute_proposer_score`, whose `calculate_committee_fraction` excludes slashed validators.
|
|
333
|
+
* Invariant per balance source within a run, but read many times per is_one_confirmed evaluation
|
|
334
|
+
* across the epoch-boundary chain walk; memoized so the full validator-set scan runs once per run.
|
|
335
|
+
*/
|
|
336
|
+
function getActiveBalances(balanceSource: FastConfirmationBalanceSource, cache: FastConfirmationCache): ActiveBalances {
|
|
337
|
+
const key: BalanceSourceCacheKey = balanceSource.state ?? balanceSource.balances;
|
|
338
|
+
const cached = cache.activeBalancesByKey.get(key);
|
|
291
339
|
if (cached !== undefined) return cached;
|
|
292
340
|
|
|
293
|
-
|
|
341
|
+
let active: ActiveBalances;
|
|
342
|
+
if (balanceSource.state) {
|
|
343
|
+
const {effectiveBalanceIncrements} = balanceSource.state;
|
|
344
|
+
const unslashedBalances = balanceSource.state.getEffectiveBalanceIncrementsZeroInactive();
|
|
345
|
+
const balances = new Uint16Array(balanceSource.state.validatorCount);
|
|
346
|
+
let total = 0;
|
|
347
|
+
let unslashedTotal = 0;
|
|
348
|
+
for (const i of balanceSource.state.getCurrentShuffling().activeIndices) {
|
|
349
|
+
balances[i] = effectiveBalanceIncrements[i];
|
|
350
|
+
total += balances[i];
|
|
351
|
+
unslashedTotal += unslashedBalances[i];
|
|
352
|
+
}
|
|
353
|
+
active = {balances, total, unslashedTotal};
|
|
354
|
+
} else {
|
|
355
|
+
// Fallback balances come from the justified-balance path, which also zeroes slashed validators
|
|
356
|
+
const total = computeTotalBalance(balanceSource.balances);
|
|
357
|
+
active = {balances: balanceSource.balances, total, unslashedTotal: total};
|
|
358
|
+
}
|
|
294
359
|
|
|
295
|
-
cache.
|
|
296
|
-
return
|
|
360
|
+
cache.activeBalancesByKey.set(key, active);
|
|
361
|
+
return active;
|
|
297
362
|
}
|
|
298
363
|
|
|
299
|
-
export function
|
|
364
|
+
export function getTotalActiveBalance(
|
|
300
365
|
balanceSource: FastConfirmationBalanceSource,
|
|
366
|
+
cache: FastConfirmationCache
|
|
367
|
+
): number {
|
|
368
|
+
return getActiveBalances(balanceSource, cache).total;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export function computeCommitteeWeightBetweenSlots(
|
|
372
|
+
ctx: FastConfirmationContext,
|
|
373
|
+
store: IFastConfirmationStore,
|
|
301
374
|
cache: FastConfirmationCache,
|
|
375
|
+
balanceSource: FastConfirmationBalanceSource,
|
|
302
376
|
startSlot: Slot,
|
|
303
377
|
endSlot: Slot
|
|
304
378
|
): bigint {
|
|
305
379
|
if (startSlot > endSlot) return 0n;
|
|
306
|
-
const totalActiveBalance = BigInt(getTotalActiveBalance(balanceSource, cache)) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
307
|
-
const startEpoch = computeEpochAtSlot(startSlot);
|
|
308
|
-
const endEpoch = computeEpochAtSlot(endSlot);
|
|
309
380
|
|
|
310
381
|
if (isFullValidatorSetCovered(startSlot, endSlot)) {
|
|
311
|
-
return
|
|
382
|
+
return BigInt(getTotalActiveBalance(balanceSource, cache)) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
312
383
|
}
|
|
313
384
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
if (
|
|
317
|
-
|
|
385
|
+
// Only the shared endpoint benefits from incremental weight prefixes across the chain walk.
|
|
386
|
+
// Other ranges reuse their compact participants without allocating a validator-sized sweep.
|
|
387
|
+
if (endSlot !== ctx.getCurrentSlot() - 1) {
|
|
388
|
+
const {balances} = getActiveBalances(balanceSource, cache);
|
|
389
|
+
let weight = 0;
|
|
390
|
+
for (const index of getSlotRangeParticipants(ctx, store, cache, startSlot, endSlot)) {
|
|
391
|
+
weight += balances[index] ?? 0;
|
|
392
|
+
}
|
|
393
|
+
return BigInt(weight) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
318
394
|
}
|
|
319
395
|
|
|
320
|
-
const
|
|
321
|
-
const
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
// the spec models overlap as:
|
|
328
|
-
// startEpochWeightProRated = startEpochWeight * (1 - numSlotsInEndEpoch / SLOTS_PER_EPOCH)
|
|
329
|
-
// = startEpochWeight * remainingSlotsInEndEpoch / SLOTS_PER_EPOCH
|
|
330
|
-
// We keep the spec's "pro-rate the start epoch" form so integer rounding matches it exactly.
|
|
331
|
-
const startEpochWeightProRated = (startEpochWeight / BigInt(SLOTS_PER_EPOCH)) * BigInt(remainingSlotsInEndEpoch);
|
|
332
|
-
|
|
333
|
-
return adjustCommitteeWeightEstimateToEnsureSafety(startEpochWeightProRated + endEpochWeight);
|
|
334
|
-
}
|
|
396
|
+
const sweep = getCommitteeSweep(ctx, store, cache, startSlot, endSlot);
|
|
397
|
+
const key: BalanceSourceCacheKey = balanceSource.state ?? balanceSource.balances;
|
|
398
|
+
let weights = sweep.weightsByKey.get(key);
|
|
399
|
+
if (weights === undefined) {
|
|
400
|
+
weights = [];
|
|
401
|
+
sweep.weightsByKey.set(key, weights);
|
|
402
|
+
}
|
|
335
403
|
|
|
336
|
-
|
|
337
|
-
|
|
404
|
+
const slotCount = endSlot - startSlot + 1;
|
|
405
|
+
if (weights.length < slotCount) {
|
|
406
|
+
const {balances} = getActiveBalances(balanceSource, cache);
|
|
407
|
+
let weight = weights.at(-1) ?? 0;
|
|
408
|
+
for (let i = weights.length; i < slotCount; i++) {
|
|
409
|
+
for (let j = i > 0 ? sweep.lengths[i - 1] : 0; j < sweep.lengths[i]; j++) {
|
|
410
|
+
weight += balances[sweep.order[j]] ?? 0;
|
|
411
|
+
}
|
|
412
|
+
weights.push(weight);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return BigInt(weights[slotCount - 1]) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
338
416
|
}
|
|
339
417
|
|
|
340
418
|
export function isFullValidatorSetCovered(startSlot: Slot, endSlot: Slot): boolean {
|
|
@@ -348,8 +426,9 @@ export function computeProposerScore(
|
|
|
348
426
|
balanceSource: FastConfirmationBalanceSource,
|
|
349
427
|
cache: FastConfirmationCache
|
|
350
428
|
): bigint {
|
|
351
|
-
const
|
|
352
|
-
|
|
429
|
+
const unslashedTotal =
|
|
430
|
+
BigInt(getActiveBalances(balanceSource, cache).unslashedTotal) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
431
|
+
const committeeWeight = unslashedTotal / BigInt(SLOTS_PER_EPOCH);
|
|
353
432
|
return (committeeWeight * BigInt(ctx.config.PROPOSER_SCORE_BOOST)) / 100n;
|
|
354
433
|
}
|
|
355
434
|
|
|
@@ -410,14 +489,14 @@ export function getNodeSupportBetweenSlots(
|
|
|
410
489
|
cache: FastConfirmationCache,
|
|
411
490
|
balanceSource: FastConfirmationBalanceSource,
|
|
412
491
|
nodeRoot: RootHex,
|
|
413
|
-
|
|
492
|
+
nodePayloadStatuses: readonly PayloadStatus[],
|
|
414
493
|
startSlot: Slot,
|
|
415
494
|
endSlot: Slot
|
|
416
495
|
): number {
|
|
417
496
|
if (startSlot > endSlot) return 0;
|
|
418
497
|
const balances = getZeroInactiveBalances(balanceSource);
|
|
419
498
|
const participants = getSlotRangeParticipants(ctx, store, cache, startSlot, endSlot);
|
|
420
|
-
if (participants.
|
|
499
|
+
if (participants.length === 0) return 0;
|
|
421
500
|
|
|
422
501
|
const equivocating = ctx.getEquivocatingIndices();
|
|
423
502
|
let score = 0;
|
|
@@ -426,7 +505,7 @@ export function getNodeSupportBetweenSlots(
|
|
|
426
505
|
if (weight === 0) continue;
|
|
427
506
|
if (equivocating.has(i)) continue;
|
|
428
507
|
const latestMessage = ctx.getLatestMessage(i);
|
|
429
|
-
if (latestMessage?.root === nodeRoot && latestMessage.payloadStatus
|
|
508
|
+
if (latestMessage?.root === nodeRoot && nodePayloadStatuses.includes(latestMessage.payloadStatus)) {
|
|
430
509
|
score += weight;
|
|
431
510
|
}
|
|
432
511
|
}
|
|
@@ -454,7 +533,7 @@ export function getEquivocationScore(
|
|
|
454
533
|
const state = balanceSource.state;
|
|
455
534
|
const stateEpoch = state ? computeEpochAtSlot(state.slot) : null;
|
|
456
535
|
const participants = getSlotRangeParticipants(ctx, store, cache, startSlot, endSlot);
|
|
457
|
-
if (participants.
|
|
536
|
+
if (participants.length === 0) return 0;
|
|
458
537
|
|
|
459
538
|
let score = 0;
|
|
460
539
|
for (const i of participants) {
|
|
@@ -477,7 +556,7 @@ export function computeAdversarialWeight(
|
|
|
477
556
|
startSlot: Slot,
|
|
478
557
|
endSlot: Slot
|
|
479
558
|
): bigint {
|
|
480
|
-
const maximumWeight =
|
|
559
|
+
const maximumWeight = computeCommitteeWeightBetweenSlots(ctx, store, cache, balanceSource, startSlot, endSlot);
|
|
481
560
|
const maxAdversarialWeight = (maximumWeight / 100n) * BigInt(ctx.config.CONFIRMATION_BYZANTINE_THRESHOLD);
|
|
482
561
|
const equivocationScore =
|
|
483
562
|
BigInt(getEquivocationScore(ctx, store, cache, balanceSource, startSlot, endSlot)) *
|
|
@@ -524,8 +603,9 @@ export function computeEmptySlotSupportDiscount(
|
|
|
524
603
|
return 0n;
|
|
525
604
|
}
|
|
526
605
|
|
|
527
|
-
// Post-Gloas the parent block has PENDING, EMPTY and FULL nodes.
|
|
528
|
-
// block
|
|
606
|
+
// Post-Gloas the parent block has PENDING, EMPTY and FULL nodes. Support for PENDING(P) and for the
|
|
607
|
+
// payload node this block extends is discounted. Support for the other payload node counts against
|
|
608
|
+
// this block. `parentBlock` is the default variant, PENDING post-Gloas and the only node pre-Gloas.
|
|
529
609
|
const parentNodePayloadStatus = ctx.getParentNodePayloadStatus(blockRoot);
|
|
530
610
|
if (parentNodePayloadStatus === null) return 0n;
|
|
531
611
|
|
|
@@ -537,7 +617,7 @@ export function computeEmptySlotSupportDiscount(
|
|
|
537
617
|
cache,
|
|
538
618
|
balanceSource,
|
|
539
619
|
block.parentRoot,
|
|
540
|
-
parentNodePayloadStatus,
|
|
620
|
+
[parentBlock.payloadStatus, parentNodePayloadStatus],
|
|
541
621
|
(parentBlock.slot + 1) as Slot,
|
|
542
622
|
(block.slot - 1) as Slot
|
|
543
623
|
)
|
|
@@ -581,21 +661,23 @@ export function computeSafetyThreshold(
|
|
|
581
661
|
// Build the threshold from the same terms used in the paper/spec:
|
|
582
662
|
// max possible committee support, proposer boost, empty-slot discount, and adversarial budget.
|
|
583
663
|
const proposerScore = computeProposerScore(ctx, balanceSource, cache);
|
|
584
|
-
const maximumSupport =
|
|
585
|
-
|
|
664
|
+
const maximumSupport = computeCommitteeWeightBetweenSlots(
|
|
665
|
+
ctx,
|
|
666
|
+
store,
|
|
586
667
|
cache,
|
|
668
|
+
balanceSource,
|
|
587
669
|
(parentBlock.slot + 1) as Slot,
|
|
588
670
|
(currentSlot - 1) as Slot
|
|
589
671
|
);
|
|
590
672
|
const supportDiscount = computeEmptySlotSupportDiscount(ctx, store, cache, balanceSource, blockRoot);
|
|
591
673
|
const adversarialWeight = getAdversarialWeight(ctx, store, cache, balanceSource, blockRoot);
|
|
592
674
|
|
|
593
|
-
//
|
|
594
|
-
//
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
675
|
+
// The discounted votes come from committees within the maximum support range and are weighed with
|
|
676
|
+
// the same balances, so exceeding it means the computation is wrong rather than an underflow to guard.
|
|
677
|
+
if (supportDiscount > maximumSupport) {
|
|
678
|
+
throw new Error(`Fast confirmation support discount ${supportDiscount} exceeds maximum support ${maximumSupport}`);
|
|
679
|
+
}
|
|
680
|
+
const threshold = (maximumSupport + proposerScore + 2n * adversarialWeight - supportDiscount) / 2n;
|
|
599
681
|
|
|
600
682
|
return {threshold, proposerScore, maximumSupport, supportDiscount, adversarialWeight};
|
|
601
683
|
}
|
|
@@ -729,9 +811,11 @@ export function computeHonestFfgSupportForCurrentTarget(
|
|
|
729
811
|
const totalActiveBalance =
|
|
730
812
|
BigInt(getTotalActiveBalance(targetBalanceSource, cache)) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
731
813
|
const ffgSupport = BigInt(getCurrentTargetScore(ctx, store, cache)) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
732
|
-
const tillNowFFGWeight =
|
|
733
|
-
|
|
814
|
+
const tillNowFFGWeight = computeCommitteeWeightBetweenSlots(
|
|
815
|
+
ctx,
|
|
816
|
+
store,
|
|
734
817
|
cache,
|
|
818
|
+
targetBalanceSource,
|
|
735
819
|
computeStartSlotAtEpoch(currentEpoch),
|
|
736
820
|
(currentSlot - 1) as Slot
|
|
737
821
|
);
|
|
@@ -1843,10 +1843,7 @@ export class ForkChoice implements IForkChoice {
|
|
|
1843
1843
|
* Child class can overwrite this for testing purpose.
|
|
1844
1844
|
*/
|
|
1845
1845
|
protected isBlockPtcTimely(block: BeaconBlock, blockDelayMs: number): boolean {
|
|
1846
|
-
const ptcThresholdMs = this.config.
|
|
1847
|
-
this.config.getForkName(block.slot),
|
|
1848
|
-
this.config.PAYLOAD_ATTESTATION_DUE_BPS
|
|
1849
|
-
);
|
|
1846
|
+
const ptcThresholdMs = this.config.getPayloadAttestationDueMs(this.config.getForkName(block.slot));
|
|
1850
1847
|
return this.fcStore.currentSlot === block.slot && blockDelayMs < ptcThresholdMs;
|
|
1851
1848
|
}
|
|
1852
1849
|
|
|
@@ -1913,7 +1910,7 @@ export class ForkChoice implements IForkChoice {
|
|
|
1913
1910
|
*
|
|
1914
1911
|
* https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.14/specs/phase0/fork-choice.md#is_proposer_equivocation
|
|
1915
1912
|
*/
|
|
1916
|
-
|
|
1913
|
+
isProposerEquivocation(block: ProtoBlock): boolean {
|
|
1917
1914
|
// Any known sibling counts, timely or not. Timeliness only matters for withholding the boost from
|
|
1918
1915
|
// the next proposer, the reorg itself is safe to attempt whenever the equivocation is visible.
|
|
1919
1916
|
return this.protoArray.hasEquivocatingBlock(block.proposerIndex, block.slot, block.blockRoot, false);
|
|
@@ -281,6 +281,8 @@ export interface IForkChoice {
|
|
|
281
281
|
shouldExtendPayload(blockRoot: RootHex): boolean;
|
|
282
282
|
/** Spec: should_build_on_full(store, head, slot) */
|
|
283
283
|
shouldBuildOnFull(head: ProtoBlock, slot: Slot): boolean;
|
|
284
|
+
/** Spec: is_proposer_equivocation(store, root) */
|
|
285
|
+
isProposerEquivocation(block: ProtoBlock): boolean;
|
|
284
286
|
/** Inclusion list verdict for the payload of `blockRoot`, as reported by the execution engine. True if it changed */
|
|
285
287
|
recordPayloadInclusionListSatisfaction(blockRoot: RootHex, satisfied: boolean): boolean;
|
|
286
288
|
/** True once the payload is delivered and not recorded unsatisfied */
|