@lodestar/fork-choice 1.52.0-dev.53c3aab4e9 → 1.52.0-dev.643dd3c5bf
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/errors.d.ts +6 -1
- package/lib/forkChoice/errors.d.ts.map +1 -1
- package/lib/forkChoice/errors.js +1 -0
- package/lib/forkChoice/errors.js.map +1 -1
- 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 +13 -15
- package/lib/forkChoice/fastConfirmation/utils.d.ts.map +1 -1
- package/lib/forkChoice/fastConfirmation/utils.js +189 -119
- package/lib/forkChoice/fastConfirmation/utils.js.map +1 -1
- package/lib/forkChoice/forkChoice.d.ts +6 -7
- package/lib/forkChoice/forkChoice.d.ts.map +1 -1
- package/lib/forkChoice/forkChoice.js +30 -26
- package/lib/forkChoice/forkChoice.js.map +1 -1
- package/lib/forkChoice/interface.d.ts +6 -7
- package/lib/forkChoice/interface.d.ts.map +1 -1
- package/lib/forkChoice/interface.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/protoArray/errors.d.ts +8 -10
- package/lib/protoArray/errors.d.ts.map +1 -1
- package/lib/protoArray/errors.js +7 -7
- package/lib/protoArray/errors.js.map +1 -1
- package/lib/protoArray/interface.d.ts +33 -2
- package/lib/protoArray/interface.d.ts.map +1 -1
- package/lib/protoArray/interface.js +22 -0
- package/lib/protoArray/interface.js.map +1 -1
- package/lib/protoArray/protoArray.d.ts +20 -22
- package/lib/protoArray/protoArray.d.ts.map +1 -1
- package/lib/protoArray/protoArray.js +104 -100
- package/lib/protoArray/protoArray.js.map +1 -1
- package/package.json +8 -8
- package/src/forkChoice/errors.ts +3 -1
- package/src/forkChoice/fastConfirmation/data.ts +3 -2
- package/src/forkChoice/fastConfirmation/types.ts +28 -4
- package/src/forkChoice/fastConfirmation/utils.ts +241 -167
- package/src/forkChoice/forkChoice.ts +31 -31
- package/src/forkChoice/interface.ts +6 -5
- package/src/index.ts +9 -1
- package/src/protoArray/errors.ts +8 -5
- package/src/protoArray/interface.ts +30 -4
- package/src/protoArray/protoArray.ts +106 -110
|
@@ -1,37 +1,36 @@
|
|
|
1
|
-
import {SLOTS_PER_EPOCH} from "@lodestar/params";
|
|
1
|
+
import {EFFECTIVE_BALANCE_INCREMENT, SLOTS_PER_EPOCH} from "@lodestar/params";
|
|
2
2
|
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
|
-
|
|
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 = 5;
|
|
26
|
+
const EFFECTIVE_BALANCE_INCREMENT_BIGINT = BigInt(EFFECTIVE_BALANCE_INCREMENT);
|
|
28
27
|
|
|
29
28
|
const SAFETY_THRESHOLD_UNREACHABLE = Object.freeze({
|
|
30
|
-
threshold:
|
|
31
|
-
proposerScore:
|
|
32
|
-
maximumSupport:
|
|
33
|
-
supportDiscount:
|
|
34
|
-
adversarialWeight:
|
|
29
|
+
threshold: 2n ** 64n,
|
|
30
|
+
proposerScore: 0n,
|
|
31
|
+
maximumSupport: 0n,
|
|
32
|
+
supportDiscount: 0n,
|
|
33
|
+
adversarialWeight: 0n,
|
|
35
34
|
});
|
|
36
35
|
|
|
37
36
|
export function getBlock(ctx: FastConfirmationContext, cache: FastConfirmationCache, root: RootHex): ProtoBlock | null {
|
|
@@ -180,25 +179,43 @@ export function getCheckpointState(
|
|
|
180
179
|
return state ?? null;
|
|
181
180
|
}
|
|
182
181
|
|
|
183
|
-
|
|
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,
|
|
184
190
|
cache: FastConfirmationCache,
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
):
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
+
}
|
|
198
215
|
}
|
|
216
|
+
sweep.lengths.push(length);
|
|
199
217
|
}
|
|
200
|
-
|
|
201
|
-
return participants;
|
|
218
|
+
return sweep;
|
|
202
219
|
}
|
|
203
220
|
|
|
204
221
|
function getSlotRangeParticipants(
|
|
@@ -207,17 +224,47 @@ function getSlotRangeParticipants(
|
|
|
207
224
|
cache: FastConfirmationCache,
|
|
208
225
|
startSlot: Slot,
|
|
209
226
|
endSlot: Slot
|
|
210
|
-
):
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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
|
+
}
|
|
217
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;
|
|
218
265
|
}
|
|
219
|
-
|
|
220
|
-
return
|
|
266
|
+
const sweep = getCommitteeSweep(ctx, store, cache, startSlot, endSlot);
|
|
267
|
+
return sweep.order.subarray(0, sweep.lengths[endSlot - startSlot]);
|
|
221
268
|
}
|
|
222
269
|
|
|
223
270
|
function isDescendantCached(
|
|
@@ -278,72 +325,94 @@ function getZeroInactiveBalances(balanceSource: FastConfirmationBalanceSource):
|
|
|
278
325
|
return balanceSource.state?.getEffectiveBalanceIncrementsZeroInactive() ?? balanceSource.balances;
|
|
279
326
|
}
|
|
280
327
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
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);
|
|
289
339
|
if (cached !== undefined) return cached;
|
|
290
340
|
|
|
291
|
-
|
|
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
|
+
}
|
|
292
359
|
|
|
293
|
-
cache.
|
|
294
|
-
return
|
|
360
|
+
cache.activeBalancesByKey.set(key, active);
|
|
361
|
+
return active;
|
|
295
362
|
}
|
|
296
363
|
|
|
297
|
-
export function
|
|
364
|
+
export function getTotalActiveBalance(
|
|
298
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,
|
|
299
374
|
cache: FastConfirmationCache,
|
|
375
|
+
balanceSource: FastConfirmationBalanceSource,
|
|
300
376
|
startSlot: Slot,
|
|
301
377
|
endSlot: Slot
|
|
302
|
-
):
|
|
303
|
-
if (startSlot > endSlot) return
|
|
304
|
-
const totalActiveBalance = getTotalActiveBalance(balanceSource, cache);
|
|
305
|
-
const startEpoch = computeEpochAtSlot(startSlot);
|
|
306
|
-
const endEpoch = computeEpochAtSlot(endSlot);
|
|
378
|
+
): bigint {
|
|
379
|
+
if (startSlot > endSlot) return 0n;
|
|
307
380
|
|
|
308
381
|
if (isFullValidatorSetCovered(startSlot, endSlot)) {
|
|
309
|
-
return
|
|
382
|
+
return BigInt(getTotalActiveBalance(balanceSource, cache)) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
310
383
|
}
|
|
311
384
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
if (
|
|
315
|
-
|
|
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;
|
|
316
394
|
}
|
|
317
395
|
|
|
318
|
-
const
|
|
319
|
-
const
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
// the spec models overlap as:
|
|
326
|
-
// startEpochWeightProRated = startEpochWeight * (1 - numSlotsInEndEpoch / SLOTS_PER_EPOCH)
|
|
327
|
-
// = startEpochWeight * remainingSlotsInEndEpoch / SLOTS_PER_EPOCH
|
|
328
|
-
// We keep the spec's "pro-rate the start epoch" form so integer rounding matches it exactly.
|
|
329
|
-
const startEpochWeightProRated = Math.floor(startEpochWeight / SLOTS_PER_EPOCH) * remainingSlotsInEndEpoch;
|
|
330
|
-
|
|
331
|
-
return adjustCommitteeWeightEstimateToEnsureSafety(startEpochWeightProRated + endEpochWeight);
|
|
332
|
-
}
|
|
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
|
+
}
|
|
333
403
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
return Math.floor((estimate * (1000 + COMMITTEE_WEIGHT_ESTIMATION_ADJUSTMENT_FACTOR) + 999) / 1000);
|
|
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;
|
|
347
416
|
}
|
|
348
417
|
|
|
349
418
|
export function isFullValidatorSetCovered(startSlot: Slot, endSlot: Slot): boolean {
|
|
@@ -356,10 +425,11 @@ export function computeProposerScore(
|
|
|
356
425
|
ctx: FastConfirmationContext,
|
|
357
426
|
balanceSource: FastConfirmationBalanceSource,
|
|
358
427
|
cache: FastConfirmationCache
|
|
359
|
-
):
|
|
360
|
-
const
|
|
361
|
-
|
|
362
|
-
|
|
428
|
+
): bigint {
|
|
429
|
+
const unslashedTotal =
|
|
430
|
+
BigInt(getActiveBalances(balanceSource, cache).unslashedTotal) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
431
|
+
const committeeWeight = unslashedTotal / BigInt(SLOTS_PER_EPOCH);
|
|
432
|
+
return (committeeWeight * BigInt(ctx.config.PROPOSER_SCORE_BOOST)) / 100n;
|
|
363
433
|
}
|
|
364
434
|
|
|
365
435
|
/**
|
|
@@ -419,14 +489,14 @@ export function getNodeSupportBetweenSlots(
|
|
|
419
489
|
cache: FastConfirmationCache,
|
|
420
490
|
balanceSource: FastConfirmationBalanceSource,
|
|
421
491
|
nodeRoot: RootHex,
|
|
422
|
-
|
|
492
|
+
nodePayloadStatuses: readonly PayloadStatus[],
|
|
423
493
|
startSlot: Slot,
|
|
424
494
|
endSlot: Slot
|
|
425
495
|
): number {
|
|
426
496
|
if (startSlot > endSlot) return 0;
|
|
427
497
|
const balances = getZeroInactiveBalances(balanceSource);
|
|
428
498
|
const participants = getSlotRangeParticipants(ctx, store, cache, startSlot, endSlot);
|
|
429
|
-
if (participants.
|
|
499
|
+
if (participants.length === 0) return 0;
|
|
430
500
|
|
|
431
501
|
const equivocating = ctx.getEquivocatingIndices();
|
|
432
502
|
let score = 0;
|
|
@@ -435,7 +505,7 @@ export function getNodeSupportBetweenSlots(
|
|
|
435
505
|
if (weight === 0) continue;
|
|
436
506
|
if (equivocating.has(i)) continue;
|
|
437
507
|
const latestMessage = ctx.getLatestMessage(i);
|
|
438
|
-
if (latestMessage?.root === nodeRoot && latestMessage.payloadStatus
|
|
508
|
+
if (latestMessage?.root === nodeRoot && nodePayloadStatuses.includes(latestMessage.payloadStatus)) {
|
|
439
509
|
score += weight;
|
|
440
510
|
}
|
|
441
511
|
}
|
|
@@ -463,7 +533,7 @@ export function getEquivocationScore(
|
|
|
463
533
|
const state = balanceSource.state;
|
|
464
534
|
const stateEpoch = state ? computeEpochAtSlot(state.slot) : null;
|
|
465
535
|
const participants = getSlotRangeParticipants(ctx, store, cache, startSlot, endSlot);
|
|
466
|
-
if (participants.
|
|
536
|
+
if (participants.length === 0) return 0;
|
|
467
537
|
|
|
468
538
|
let score = 0;
|
|
469
539
|
for (const i of participants) {
|
|
@@ -485,14 +555,13 @@ export function computeAdversarialWeight(
|
|
|
485
555
|
balanceSource: FastConfirmationBalanceSource,
|
|
486
556
|
startSlot: Slot,
|
|
487
557
|
endSlot: Slot
|
|
488
|
-
):
|
|
489
|
-
const maximumWeight =
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
return maxAdversarialWeight > equivocationScore ? maxAdversarialWeight - equivocationScore : 0;
|
|
558
|
+
): bigint {
|
|
559
|
+
const maximumWeight = computeCommitteeWeightBetweenSlots(ctx, store, cache, balanceSource, startSlot, endSlot);
|
|
560
|
+
const maxAdversarialWeight = (maximumWeight / 100n) * BigInt(ctx.config.CONFIRMATION_BYZANTINE_THRESHOLD);
|
|
561
|
+
const equivocationScore =
|
|
562
|
+
BigInt(getEquivocationScore(ctx, store, cache, balanceSource, startSlot, endSlot)) *
|
|
563
|
+
EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
564
|
+
return maxAdversarialWeight > equivocationScore ? maxAdversarialWeight - equivocationScore : 0n;
|
|
496
565
|
}
|
|
497
566
|
|
|
498
567
|
export function getAdversarialWeight(
|
|
@@ -501,13 +570,13 @@ export function getAdversarialWeight(
|
|
|
501
570
|
cache: FastConfirmationCache,
|
|
502
571
|
balanceSource: FastConfirmationBalanceSource,
|
|
503
572
|
blockRoot: RootHex
|
|
504
|
-
):
|
|
573
|
+
): bigint {
|
|
505
574
|
const currentSlot = ctx.getCurrentSlot();
|
|
506
|
-
if (currentSlot === 0) return
|
|
575
|
+
if (currentSlot === 0) return 0n;
|
|
507
576
|
const block = getBlock(ctx, cache, blockRoot);
|
|
508
|
-
if (!block) return
|
|
577
|
+
if (!block) return 0n;
|
|
509
578
|
const parentBlock = getBlock(ctx, cache, block.parentRoot);
|
|
510
|
-
if (!parentBlock) return
|
|
579
|
+
if (!parentBlock) return 0n;
|
|
511
580
|
const blockEpoch = computeEpochAtSlot(block.slot);
|
|
512
581
|
const parentEpoch = computeEpochAtSlot(parentBlock.slot);
|
|
513
582
|
|
|
@@ -524,31 +593,35 @@ export function computeEmptySlotSupportDiscount(
|
|
|
524
593
|
cache: FastConfirmationCache,
|
|
525
594
|
balanceSource: FastConfirmationBalanceSource,
|
|
526
595
|
blockRoot: RootHex
|
|
527
|
-
):
|
|
596
|
+
): bigint {
|
|
528
597
|
const block = getBlock(ctx, cache, blockRoot);
|
|
529
|
-
if (!block) return
|
|
598
|
+
if (!block) return 0n;
|
|
530
599
|
const parentBlock = getBlock(ctx, cache, block.parentRoot);
|
|
531
|
-
if (!parentBlock) return
|
|
600
|
+
if (!parentBlock) return 0n;
|
|
532
601
|
|
|
533
602
|
if (parentBlock.slot + 1 === block.slot) {
|
|
534
|
-
return
|
|
603
|
+
return 0n;
|
|
535
604
|
}
|
|
536
605
|
|
|
537
|
-
// Post-Gloas the parent block has PENDING, EMPTY and FULL nodes.
|
|
538
|
-
// 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.
|
|
539
609
|
const parentNodePayloadStatus = ctx.getParentNodePayloadStatus(blockRoot);
|
|
540
|
-
if (parentNodePayloadStatus === null) return
|
|
541
|
-
|
|
542
|
-
const parentSupportInEmptySlots =
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
610
|
+
if (parentNodePayloadStatus === null) return 0n;
|
|
611
|
+
|
|
612
|
+
const parentSupportInEmptySlots =
|
|
613
|
+
BigInt(
|
|
614
|
+
getNodeSupportBetweenSlots(
|
|
615
|
+
ctx,
|
|
616
|
+
store,
|
|
617
|
+
cache,
|
|
618
|
+
balanceSource,
|
|
619
|
+
block.parentRoot,
|
|
620
|
+
[parentBlock.payloadStatus, parentNodePayloadStatus],
|
|
621
|
+
(parentBlock.slot + 1) as Slot,
|
|
622
|
+
(block.slot - 1) as Slot
|
|
623
|
+
)
|
|
624
|
+
) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
552
625
|
const adversarialWeight = computeAdversarialWeight(
|
|
553
626
|
ctx,
|
|
554
627
|
store,
|
|
@@ -558,7 +631,7 @@ export function computeEmptySlotSupportDiscount(
|
|
|
558
631
|
(block.slot - 1) as Slot
|
|
559
632
|
);
|
|
560
633
|
|
|
561
|
-
return parentSupportInEmptySlots > adversarialWeight ? parentSupportInEmptySlots - adversarialWeight :
|
|
634
|
+
return parentSupportInEmptySlots > adversarialWeight ? parentSupportInEmptySlots - adversarialWeight : 0n;
|
|
562
635
|
}
|
|
563
636
|
|
|
564
637
|
export function computeSafetyThreshold(
|
|
@@ -568,11 +641,11 @@ export function computeSafetyThreshold(
|
|
|
568
641
|
balanceSource: FastConfirmationBalanceSource,
|
|
569
642
|
blockRoot: RootHex
|
|
570
643
|
): {
|
|
571
|
-
threshold:
|
|
572
|
-
proposerScore:
|
|
573
|
-
maximumSupport:
|
|
574
|
-
supportDiscount:
|
|
575
|
-
adversarialWeight:
|
|
644
|
+
threshold: bigint;
|
|
645
|
+
proposerScore: bigint;
|
|
646
|
+
maximumSupport: bigint;
|
|
647
|
+
supportDiscount: bigint;
|
|
648
|
+
adversarialWeight: bigint;
|
|
576
649
|
} {
|
|
577
650
|
const currentSlot = ctx.getCurrentSlot();
|
|
578
651
|
const block = getBlock(ctx, cache, blockRoot);
|
|
@@ -588,21 +661,23 @@ export function computeSafetyThreshold(
|
|
|
588
661
|
// Build the threshold from the same terms used in the paper/spec:
|
|
589
662
|
// max possible committee support, proposer boost, empty-slot discount, and adversarial budget.
|
|
590
663
|
const proposerScore = computeProposerScore(ctx, balanceSource, cache);
|
|
591
|
-
const maximumSupport =
|
|
592
|
-
|
|
664
|
+
const maximumSupport = computeCommitteeWeightBetweenSlots(
|
|
665
|
+
ctx,
|
|
666
|
+
store,
|
|
593
667
|
cache,
|
|
668
|
+
balanceSource,
|
|
594
669
|
(parentBlock.slot + 1) as Slot,
|
|
595
670
|
(currentSlot - 1) as Slot
|
|
596
671
|
);
|
|
597
672
|
const supportDiscount = computeEmptySlotSupportDiscount(ctx, store, cache, balanceSource, blockRoot);
|
|
598
673
|
const adversarialWeight = getAdversarialWeight(ctx, store, cache, balanceSource, blockRoot);
|
|
599
674
|
|
|
600
|
-
//
|
|
601
|
-
//
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
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;
|
|
606
681
|
|
|
607
682
|
return {threshold, proposerScore, maximumSupport, supportDiscount, adversarialWeight};
|
|
608
683
|
}
|
|
@@ -626,7 +701,8 @@ export function isOneConfirmed(
|
|
|
626
701
|
|
|
627
702
|
// Spec: is_one_confirmed(store, balance_source, block_root)
|
|
628
703
|
// Compare actual support for this block against the computed LMD-GHOST safety threshold.
|
|
629
|
-
const support =
|
|
704
|
+
const support =
|
|
705
|
+
BigInt(getAttestationScore(ctx, cache, balanceSource, blockRoot, sourceKey)) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
630
706
|
const {threshold, proposerScore, maximumSupport, supportDiscount, adversarialWeight} = computeSafetyThreshold(
|
|
631
707
|
ctx,
|
|
632
708
|
store,
|
|
@@ -709,18 +785,15 @@ export function getCurrentTargetScore(
|
|
|
709
785
|
}
|
|
710
786
|
|
|
711
787
|
function computeHonestFfgSupport(
|
|
712
|
-
totalActiveBalance:
|
|
713
|
-
ffgSupport:
|
|
714
|
-
ffgWeightTillNow:
|
|
715
|
-
adversarialWeight:
|
|
788
|
+
totalActiveBalance: bigint,
|
|
789
|
+
ffgSupport: bigint,
|
|
790
|
+
ffgWeightTillNow: bigint,
|
|
791
|
+
adversarialWeight: bigint,
|
|
716
792
|
byzantineThreshold: number
|
|
717
|
-
):
|
|
793
|
+
): bigint {
|
|
718
794
|
const remainingFfgWeight = totalActiveBalance - ffgWeightTillNow;
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
// to preserve the same percentage semantics at our coarser unit scale.
|
|
722
|
-
const remainingHonestFfgWeight = Math.floor((remainingFfgWeight * (100 - byzantineThreshold)) / 100);
|
|
723
|
-
const minHonestFfgSupport = ffgSupport - Math.min(adversarialWeight, ffgSupport);
|
|
795
|
+
const remainingHonestFfgWeight = (remainingFfgWeight / 100n) * BigInt(100 - byzantineThreshold);
|
|
796
|
+
const minHonestFfgSupport = ffgSupport > adversarialWeight ? ffgSupport - adversarialWeight : 0n;
|
|
724
797
|
return minHonestFfgSupport + remainingHonestFfgWeight;
|
|
725
798
|
}
|
|
726
799
|
|
|
@@ -728,18 +801,21 @@ export function computeHonestFfgSupportForCurrentTarget(
|
|
|
728
801
|
ctx: FastConfirmationContext,
|
|
729
802
|
store: IFastConfirmationStore,
|
|
730
803
|
cache: FastConfirmationCache
|
|
731
|
-
):
|
|
804
|
+
): bigint {
|
|
732
805
|
const currentSlot = ctx.getCurrentSlot();
|
|
733
|
-
if (currentSlot === 0) return
|
|
806
|
+
if (currentSlot === 0) return 0n;
|
|
734
807
|
const currentEpoch = computeEpochAtSlot(currentSlot);
|
|
735
808
|
const targetState = getCurrentEpochState(ctx, store, cache);
|
|
736
|
-
if (!targetState) return
|
|
809
|
+
if (!targetState) return 0n;
|
|
737
810
|
const targetBalanceSource = {state: targetState, balances: targetState.effectiveBalanceIncrements};
|
|
738
|
-
const totalActiveBalance =
|
|
739
|
-
|
|
740
|
-
const
|
|
741
|
-
|
|
811
|
+
const totalActiveBalance =
|
|
812
|
+
BigInt(getTotalActiveBalance(targetBalanceSource, cache)) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
813
|
+
const ffgSupport = BigInt(getCurrentTargetScore(ctx, store, cache)) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
814
|
+
const tillNowFFGWeight = computeCommitteeWeightBetweenSlots(
|
|
815
|
+
ctx,
|
|
816
|
+
store,
|
|
742
817
|
cache,
|
|
818
|
+
targetBalanceSource,
|
|
743
819
|
computeStartSlotAtEpoch(currentEpoch),
|
|
744
820
|
(currentSlot - 1) as Slot
|
|
745
821
|
);
|
|
@@ -772,12 +848,11 @@ export function willNoConflictingCheckpointBeJustified(
|
|
|
772
848
|
}
|
|
773
849
|
const targetState = getCurrentEpochState(ctx, store, cache);
|
|
774
850
|
if (!targetState) return false;
|
|
775
|
-
const totalActiveBalance =
|
|
776
|
-
{state: targetState, balances: targetState.effectiveBalanceIncrements},
|
|
777
|
-
|
|
778
|
-
);
|
|
851
|
+
const totalActiveBalance =
|
|
852
|
+
BigInt(getTotalActiveBalance({state: targetState, balances: targetState.effectiveBalanceIncrements}, cache)) *
|
|
853
|
+
EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
779
854
|
const honestSupport = computeHonestFfgSupportForCurrentTarget(ctx, store, cache);
|
|
780
|
-
return
|
|
855
|
+
return 3n * honestSupport > 1n * totalActiveBalance;
|
|
781
856
|
}
|
|
782
857
|
|
|
783
858
|
export function willCurrentTargetBeJustified(
|
|
@@ -787,12 +862,11 @@ export function willCurrentTargetBeJustified(
|
|
|
787
862
|
): boolean {
|
|
788
863
|
const targetState = getCurrentEpochState(ctx, store, cache);
|
|
789
864
|
if (!targetState) return false;
|
|
790
|
-
const totalActiveBalance =
|
|
791
|
-
{state: targetState, balances: targetState.effectiveBalanceIncrements},
|
|
792
|
-
|
|
793
|
-
);
|
|
865
|
+
const totalActiveBalance =
|
|
866
|
+
BigInt(getTotalActiveBalance({state: targetState, balances: targetState.effectiveBalanceIncrements}, cache)) *
|
|
867
|
+
EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
794
868
|
const honestSupport = computeHonestFfgSupportForCurrentTarget(ctx, store, cache);
|
|
795
|
-
return
|
|
869
|
+
return 3n * honestSupport >= 2n * totalActiveBalance;
|
|
796
870
|
}
|
|
797
871
|
|
|
798
872
|
export function isConfirmedChainSafe(
|