@lodestar/fork-choice 1.52.0-dev.f3cc451977 → 1.52.0-dev.f51cb4156c
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 +12 -14
- package/lib/forkChoice/fastConfirmation/utils.d.ts.map +1 -1
- package/lib/forkChoice/fastConfirmation/utils.js +184 -115
- 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 +236 -163
- 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
|
/**
|
|
@@ -426,7 +496,7 @@ export function getNodeSupportBetweenSlots(
|
|
|
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;
|
|
@@ -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,34 @@ 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
606
|
// Post-Gloas the parent block has PENDING, EMPTY and FULL nodes. Only the support of the node this
|
|
538
607
|
// block actually extends is discounted; votes for the other parent nodes are not support for it.
|
|
539
608
|
const parentNodePayloadStatus = ctx.getParentNodePayloadStatus(blockRoot);
|
|
540
|
-
if (parentNodePayloadStatus === null) return
|
|
541
|
-
|
|
542
|
-
const parentSupportInEmptySlots =
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
609
|
+
if (parentNodePayloadStatus === null) return 0n;
|
|
610
|
+
|
|
611
|
+
const parentSupportInEmptySlots =
|
|
612
|
+
BigInt(
|
|
613
|
+
getNodeSupportBetweenSlots(
|
|
614
|
+
ctx,
|
|
615
|
+
store,
|
|
616
|
+
cache,
|
|
617
|
+
balanceSource,
|
|
618
|
+
block.parentRoot,
|
|
619
|
+
parentNodePayloadStatus,
|
|
620
|
+
(parentBlock.slot + 1) as Slot,
|
|
621
|
+
(block.slot - 1) as Slot
|
|
622
|
+
)
|
|
623
|
+
) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
552
624
|
const adversarialWeight = computeAdversarialWeight(
|
|
553
625
|
ctx,
|
|
554
626
|
store,
|
|
@@ -558,7 +630,7 @@ export function computeEmptySlotSupportDiscount(
|
|
|
558
630
|
(block.slot - 1) as Slot
|
|
559
631
|
);
|
|
560
632
|
|
|
561
|
-
return parentSupportInEmptySlots > adversarialWeight ? parentSupportInEmptySlots - adversarialWeight :
|
|
633
|
+
return parentSupportInEmptySlots > adversarialWeight ? parentSupportInEmptySlots - adversarialWeight : 0n;
|
|
562
634
|
}
|
|
563
635
|
|
|
564
636
|
export function computeSafetyThreshold(
|
|
@@ -568,11 +640,11 @@ export function computeSafetyThreshold(
|
|
|
568
640
|
balanceSource: FastConfirmationBalanceSource,
|
|
569
641
|
blockRoot: RootHex
|
|
570
642
|
): {
|
|
571
|
-
threshold:
|
|
572
|
-
proposerScore:
|
|
573
|
-
maximumSupport:
|
|
574
|
-
supportDiscount:
|
|
575
|
-
adversarialWeight:
|
|
643
|
+
threshold: bigint;
|
|
644
|
+
proposerScore: bigint;
|
|
645
|
+
maximumSupport: bigint;
|
|
646
|
+
supportDiscount: bigint;
|
|
647
|
+
adversarialWeight: bigint;
|
|
576
648
|
} {
|
|
577
649
|
const currentSlot = ctx.getCurrentSlot();
|
|
578
650
|
const block = getBlock(ctx, cache, blockRoot);
|
|
@@ -588,21 +660,23 @@ export function computeSafetyThreshold(
|
|
|
588
660
|
// Build the threshold from the same terms used in the paper/spec:
|
|
589
661
|
// max possible committee support, proposer boost, empty-slot discount, and adversarial budget.
|
|
590
662
|
const proposerScore = computeProposerScore(ctx, balanceSource, cache);
|
|
591
|
-
const maximumSupport =
|
|
592
|
-
|
|
663
|
+
const maximumSupport = computeCommitteeWeightBetweenSlots(
|
|
664
|
+
ctx,
|
|
665
|
+
store,
|
|
593
666
|
cache,
|
|
667
|
+
balanceSource,
|
|
594
668
|
(parentBlock.slot + 1) as Slot,
|
|
595
669
|
(currentSlot - 1) as Slot
|
|
596
670
|
);
|
|
597
671
|
const supportDiscount = computeEmptySlotSupportDiscount(ctx, store, cache, balanceSource, blockRoot);
|
|
598
672
|
const adversarialWeight = getAdversarialWeight(ctx, store, cache, balanceSource, blockRoot);
|
|
599
673
|
|
|
600
|
-
//
|
|
601
|
-
//
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
674
|
+
// The discounted votes come from committees within the maximum support range and are weighed with
|
|
675
|
+
// the same balances, so exceeding it means the computation is wrong rather than an underflow to guard.
|
|
676
|
+
if (supportDiscount > maximumSupport) {
|
|
677
|
+
throw new Error(`Fast confirmation support discount ${supportDiscount} exceeds maximum support ${maximumSupport}`);
|
|
678
|
+
}
|
|
679
|
+
const threshold = (maximumSupport + proposerScore + 2n * adversarialWeight - supportDiscount) / 2n;
|
|
606
680
|
|
|
607
681
|
return {threshold, proposerScore, maximumSupport, supportDiscount, adversarialWeight};
|
|
608
682
|
}
|
|
@@ -626,7 +700,8 @@ export function isOneConfirmed(
|
|
|
626
700
|
|
|
627
701
|
// Spec: is_one_confirmed(store, balance_source, block_root)
|
|
628
702
|
// Compare actual support for this block against the computed LMD-GHOST safety threshold.
|
|
629
|
-
const support =
|
|
703
|
+
const support =
|
|
704
|
+
BigInt(getAttestationScore(ctx, cache, balanceSource, blockRoot, sourceKey)) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
630
705
|
const {threshold, proposerScore, maximumSupport, supportDiscount, adversarialWeight} = computeSafetyThreshold(
|
|
631
706
|
ctx,
|
|
632
707
|
store,
|
|
@@ -709,18 +784,15 @@ export function getCurrentTargetScore(
|
|
|
709
784
|
}
|
|
710
785
|
|
|
711
786
|
function computeHonestFfgSupport(
|
|
712
|
-
totalActiveBalance:
|
|
713
|
-
ffgSupport:
|
|
714
|
-
ffgWeightTillNow:
|
|
715
|
-
adversarialWeight:
|
|
787
|
+
totalActiveBalance: bigint,
|
|
788
|
+
ffgSupport: bigint,
|
|
789
|
+
ffgWeightTillNow: bigint,
|
|
790
|
+
adversarialWeight: bigint,
|
|
716
791
|
byzantineThreshold: number
|
|
717
|
-
):
|
|
792
|
+
): bigint {
|
|
718
793
|
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);
|
|
794
|
+
const remainingHonestFfgWeight = (remainingFfgWeight / 100n) * BigInt(100 - byzantineThreshold);
|
|
795
|
+
const minHonestFfgSupport = ffgSupport > adversarialWeight ? ffgSupport - adversarialWeight : 0n;
|
|
724
796
|
return minHonestFfgSupport + remainingHonestFfgWeight;
|
|
725
797
|
}
|
|
726
798
|
|
|
@@ -728,18 +800,21 @@ export function computeHonestFfgSupportForCurrentTarget(
|
|
|
728
800
|
ctx: FastConfirmationContext,
|
|
729
801
|
store: IFastConfirmationStore,
|
|
730
802
|
cache: FastConfirmationCache
|
|
731
|
-
):
|
|
803
|
+
): bigint {
|
|
732
804
|
const currentSlot = ctx.getCurrentSlot();
|
|
733
|
-
if (currentSlot === 0) return
|
|
805
|
+
if (currentSlot === 0) return 0n;
|
|
734
806
|
const currentEpoch = computeEpochAtSlot(currentSlot);
|
|
735
807
|
const targetState = getCurrentEpochState(ctx, store, cache);
|
|
736
|
-
if (!targetState) return
|
|
808
|
+
if (!targetState) return 0n;
|
|
737
809
|
const targetBalanceSource = {state: targetState, balances: targetState.effectiveBalanceIncrements};
|
|
738
|
-
const totalActiveBalance =
|
|
739
|
-
|
|
740
|
-
const
|
|
741
|
-
|
|
810
|
+
const totalActiveBalance =
|
|
811
|
+
BigInt(getTotalActiveBalance(targetBalanceSource, cache)) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
812
|
+
const ffgSupport = BigInt(getCurrentTargetScore(ctx, store, cache)) * EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
813
|
+
const tillNowFFGWeight = computeCommitteeWeightBetweenSlots(
|
|
814
|
+
ctx,
|
|
815
|
+
store,
|
|
742
816
|
cache,
|
|
817
|
+
targetBalanceSource,
|
|
743
818
|
computeStartSlotAtEpoch(currentEpoch),
|
|
744
819
|
(currentSlot - 1) as Slot
|
|
745
820
|
);
|
|
@@ -772,12 +847,11 @@ export function willNoConflictingCheckpointBeJustified(
|
|
|
772
847
|
}
|
|
773
848
|
const targetState = getCurrentEpochState(ctx, store, cache);
|
|
774
849
|
if (!targetState) return false;
|
|
775
|
-
const totalActiveBalance =
|
|
776
|
-
{state: targetState, balances: targetState.effectiveBalanceIncrements},
|
|
777
|
-
|
|
778
|
-
);
|
|
850
|
+
const totalActiveBalance =
|
|
851
|
+
BigInt(getTotalActiveBalance({state: targetState, balances: targetState.effectiveBalanceIncrements}, cache)) *
|
|
852
|
+
EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
779
853
|
const honestSupport = computeHonestFfgSupportForCurrentTarget(ctx, store, cache);
|
|
780
|
-
return
|
|
854
|
+
return 3n * honestSupport > 1n * totalActiveBalance;
|
|
781
855
|
}
|
|
782
856
|
|
|
783
857
|
export function willCurrentTargetBeJustified(
|
|
@@ -787,12 +861,11 @@ export function willCurrentTargetBeJustified(
|
|
|
787
861
|
): boolean {
|
|
788
862
|
const targetState = getCurrentEpochState(ctx, store, cache);
|
|
789
863
|
if (!targetState) return false;
|
|
790
|
-
const totalActiveBalance =
|
|
791
|
-
{state: targetState, balances: targetState.effectiveBalanceIncrements},
|
|
792
|
-
|
|
793
|
-
);
|
|
864
|
+
const totalActiveBalance =
|
|
865
|
+
BigInt(getTotalActiveBalance({state: targetState, balances: targetState.effectiveBalanceIncrements}, cache)) *
|
|
866
|
+
EFFECTIVE_BALANCE_INCREMENT_BIGINT;
|
|
794
867
|
const honestSupport = computeHonestFfgSupportForCurrentTarget(ctx, store, cache);
|
|
795
|
-
return
|
|
868
|
+
return 3n * honestSupport >= 2n * totalActiveBalance;
|
|
796
869
|
}
|
|
797
870
|
|
|
798
871
|
export function isConfirmedChainSafe(
|