@lodestar/state-transition 1.39.0-rc.4 → 1.40.0-dev.3be9500fa9
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/block/isValidIndexedAttestation.d.ts +4 -5
- package/lib/block/isValidIndexedAttestation.d.ts.map +1 -1
- package/lib/block/isValidIndexedAttestation.js +9 -10
- package/lib/block/isValidIndexedAttestation.js.map +1 -1
- package/lib/block/processAttestationPhase0.d.ts.map +1 -1
- package/lib/block/processAttestationPhase0.js +1 -1
- package/lib/block/processAttestationPhase0.js.map +1 -1
- package/lib/block/processAttesterSlashing.d.ts +3 -2
- package/lib/block/processAttesterSlashing.d.ts.map +1 -1
- package/lib/block/processAttesterSlashing.js +3 -3
- package/lib/block/processAttesterSlashing.js.map +1 -1
- package/lib/block/processBlsToExecutionChange.d.ts +3 -1
- package/lib/block/processBlsToExecutionChange.d.ts.map +1 -1
- package/lib/block/processBlsToExecutionChange.js +7 -11
- package/lib/block/processBlsToExecutionChange.js.map +1 -1
- package/lib/block/processProposerSlashing.d.ts +5 -2
- package/lib/block/processProposerSlashing.d.ts.map +1 -1
- package/lib/block/processProposerSlashing.js +7 -5
- package/lib/block/processProposerSlashing.js.map +1 -1
- package/lib/cache/epochCache.d.ts +8 -28
- package/lib/cache/epochCache.d.ts.map +1 -1
- package/lib/cache/epochCache.js +40 -180
- package/lib/cache/epochCache.js.map +1 -1
- package/lib/cache/epochTransitionCache.d.ts +5 -12
- package/lib/cache/epochTransitionCache.d.ts.map +1 -1
- package/lib/cache/epochTransitionCache.js +4 -14
- package/lib/cache/epochTransitionCache.js.map +1 -1
- package/lib/cache/stateCache.d.ts.map +1 -1
- package/lib/cache/stateCache.js +1 -2
- package/lib/cache/stateCache.js.map +1 -1
- package/lib/epoch/processProposerLookahead.d.ts.map +1 -1
- package/lib/epoch/processProposerLookahead.js +3 -6
- package/lib/epoch/processProposerLookahead.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/rewards/blockRewards.d.ts +2 -1
- package/lib/rewards/blockRewards.d.ts.map +1 -1
- package/lib/rewards/blockRewards.js +3 -2
- package/lib/rewards/blockRewards.js.map +1 -1
- package/lib/rewards/syncCommitteeRewards.d.ts.map +1 -1
- package/lib/rewards/syncCommitteeRewards.js +10 -11
- package/lib/rewards/syncCommitteeRewards.js.map +1 -1
- package/lib/signatureSets/blsToExecutionChange.d.ts +1 -2
- package/lib/signatureSets/blsToExecutionChange.d.ts.map +1 -1
- package/lib/signatureSets/blsToExecutionChange.js +2 -2
- package/lib/signatureSets/blsToExecutionChange.js.map +1 -1
- package/lib/types.d.ts +1 -1
- package/lib/types.d.ts.map +1 -1
- package/lib/util/epochShuffling.d.ts +1 -34
- package/lib/util/epochShuffling.d.ts.map +1 -1
- package/lib/util/epochShuffling.js +1 -1
- package/lib/util/epochShuffling.js.map +1 -1
- package/lib/util/index.d.ts +1 -2
- package/lib/util/index.d.ts.map +1 -1
- package/lib/util/index.js +1 -2
- package/lib/util/index.js.map +1 -1
- package/lib/util/shuffling.d.ts +58 -0
- package/lib/util/shuffling.d.ts.map +1 -0
- package/lib/util/shuffling.js +175 -0
- package/lib/util/shuffling.js.map +1 -0
- package/package.json +7 -7
- package/src/block/isValidIndexedAttestation.ts +17 -12
- package/src/block/processAttestationPhase0.ts +2 -1
- package/src/block/processAttesterSlashing.ts +16 -4
- package/src/block/processBlsToExecutionChange.ts +13 -14
- package/src/block/processProposerSlashing.ts +21 -11
- package/src/cache/epochCache.ts +52 -214
- package/src/cache/epochTransitionCache.ts +9 -34
- package/src/cache/stateCache.ts +1 -2
- package/src/epoch/processProposerLookahead.ts +3 -7
- package/src/index.ts +0 -2
- package/src/rewards/blockRewards.ts +6 -3
- package/src/rewards/syncCommitteeRewards.ts +10 -13
- package/src/signatureSets/blsToExecutionChange.ts +2 -3
- package/src/types.ts +1 -0
- package/src/util/epochShuffling.ts +2 -43
- package/src/util/index.ts +1 -2
- package/src/util/shuffling.ts +234 -0
- package/lib/util/calculateCommitteeAssignments.d.ts +0 -12
- package/lib/util/calculateCommitteeAssignments.d.ts.map +0 -1
- package/lib/util/calculateCommitteeAssignments.js +0 -26
- package/lib/util/calculateCommitteeAssignments.js.map +0 -1
- package/lib/util/shufflingDecisionRoot.d.ts +0 -20
- package/lib/util/shufflingDecisionRoot.d.ts.map +0 -1
- package/lib/util/shufflingDecisionRoot.js +0 -76
- package/lib/util/shufflingDecisionRoot.js.map +0 -1
- package/src/util/calculateCommitteeAssignments.ts +0 -43
- package/src/util/shufflingDecisionRoot.ts +0 -81
package/src/cache/epochCache.ts
CHANGED
|
@@ -23,16 +23,13 @@ import {
|
|
|
23
23
|
SubnetID,
|
|
24
24
|
SyncPeriod,
|
|
25
25
|
ValidatorIndex,
|
|
26
|
-
electra,
|
|
27
26
|
gloas,
|
|
28
|
-
phase0,
|
|
29
27
|
} from "@lodestar/types";
|
|
30
28
|
import {LodestarError} from "@lodestar/utils";
|
|
31
29
|
import {getTotalSlashingsByIncrement} from "../epoch/processSlashings.js";
|
|
32
|
-
import {AttesterDuty, calculateCommitteeAssignments} from "../util/calculateCommitteeAssignments.js";
|
|
33
30
|
import {
|
|
34
31
|
EpochShuffling,
|
|
35
|
-
|
|
32
|
+
calculateDecisionRoot,
|
|
36
33
|
calculateShufflingDecisionRoot,
|
|
37
34
|
computeEpochShuffling,
|
|
38
35
|
} from "../util/epochShuffling.js";
|
|
@@ -40,7 +37,6 @@ import {
|
|
|
40
37
|
computeActivationExitEpoch,
|
|
41
38
|
computeEpochAtSlot,
|
|
42
39
|
computeProposers,
|
|
43
|
-
computeStartSlotAtEpoch,
|
|
44
40
|
computeSyncPeriodAtEpoch,
|
|
45
41
|
getActivationChurnLimit,
|
|
46
42
|
getChurnLimit,
|
|
@@ -49,6 +45,13 @@ import {
|
|
|
49
45
|
isAggregatorFromCommitteeLength,
|
|
50
46
|
naiveGetPayloadTimlinessCommitteeIndices,
|
|
51
47
|
} from "../util/index.js";
|
|
48
|
+
import {
|
|
49
|
+
AttesterDuty,
|
|
50
|
+
calculateCommitteeAssignments,
|
|
51
|
+
getAttestingIndices,
|
|
52
|
+
getBeaconCommittees,
|
|
53
|
+
getIndexedAttestation,
|
|
54
|
+
} from "../util/shuffling.js";
|
|
52
55
|
import {computeBaseRewardPerIncrement, computeSyncParticipantReward} from "../util/syncCommittee.js";
|
|
53
56
|
import {sumTargetUnslashedBalanceIncrements} from "../util/targetUnslashedBalance.js";
|
|
54
57
|
import {EffectiveBalanceIncrements, getEffectiveBalanceIncrementsWithLen} from "./effectiveBalanceIncrements.js";
|
|
@@ -61,7 +64,7 @@ import {
|
|
|
61
64
|
computeSyncCommitteeCache,
|
|
62
65
|
getSyncCommitteeCache,
|
|
63
66
|
} from "./syncCommitteeCache.js";
|
|
64
|
-
import {BeaconStateAllForks, BeaconStateAltair, BeaconStateGloas} from "./types.js";
|
|
67
|
+
import {BeaconStateAllForks, BeaconStateAltair, BeaconStateGloas, ShufflingGetter} from "./types.js";
|
|
65
68
|
|
|
66
69
|
/** `= PROPOSER_WEIGHT / (WEIGHT_DENOMINATOR - PROPOSER_WEIGHT)` */
|
|
67
70
|
export const PROPOSER_WEIGHT_FACTOR = PROPOSER_WEIGHT / (WEIGHT_DENOMINATOR - PROPOSER_WEIGHT);
|
|
@@ -70,12 +73,12 @@ export type EpochCacheImmutableData = {
|
|
|
70
73
|
config: BeaconConfig;
|
|
71
74
|
pubkey2index: PubkeyIndexMap;
|
|
72
75
|
index2pubkey: Index2PubkeyCache;
|
|
73
|
-
shufflingCache?: IShufflingCache;
|
|
74
76
|
};
|
|
75
77
|
|
|
76
78
|
export type EpochCacheOpts = {
|
|
77
79
|
skipSyncCommitteeCache?: boolean;
|
|
78
80
|
skipSyncPubkeys?: boolean;
|
|
81
|
+
shufflingGetter?: ShufflingGetter;
|
|
79
82
|
};
|
|
80
83
|
|
|
81
84
|
/** Defers computing proposers by persisting only the seed, and dropping it once indexes are computed */
|
|
@@ -117,12 +120,6 @@ export class EpochCache {
|
|
|
117
120
|
* $VALIDATOR_COUNT x BLST deserialized pubkey (Jacobian coordinates)
|
|
118
121
|
*/
|
|
119
122
|
index2pubkey: Index2PubkeyCache;
|
|
120
|
-
/**
|
|
121
|
-
* ShufflingCache is passed in from `beacon-node` so should be available at runtime but may not be
|
|
122
|
-
* present during testing.
|
|
123
|
-
*/
|
|
124
|
-
shufflingCache?: IShufflingCache;
|
|
125
|
-
|
|
126
123
|
/**
|
|
127
124
|
* Indexes of the block proposers for the current epoch.
|
|
128
125
|
* For pre-fulu, this is computed and cached from the current shuffling.
|
|
@@ -161,7 +158,7 @@ export class EpochCache {
|
|
|
161
158
|
/** Same as previousShuffling */
|
|
162
159
|
currentShuffling: EpochShuffling;
|
|
163
160
|
/** Same as previousShuffling */
|
|
164
|
-
nextShuffling: EpochShuffling
|
|
161
|
+
nextShuffling: EpochShuffling;
|
|
165
162
|
/**
|
|
166
163
|
* Cache nextActiveIndices so that in afterProcessEpoch the next shuffling can be build synchronously
|
|
167
164
|
* in case it is not built or the ShufflingCache is not available
|
|
@@ -254,7 +251,6 @@ export class EpochCache {
|
|
|
254
251
|
config: BeaconConfig;
|
|
255
252
|
pubkey2index: PubkeyIndexMap;
|
|
256
253
|
index2pubkey: Index2PubkeyCache;
|
|
257
|
-
shufflingCache?: IShufflingCache;
|
|
258
254
|
proposers: number[];
|
|
259
255
|
proposersPrevEpoch: number[] | null;
|
|
260
256
|
proposersNextEpoch: ProposersDeferred;
|
|
@@ -263,7 +259,7 @@ export class EpochCache {
|
|
|
263
259
|
nextDecisionRoot: RootHex;
|
|
264
260
|
previousShuffling: EpochShuffling;
|
|
265
261
|
currentShuffling: EpochShuffling;
|
|
266
|
-
nextShuffling: EpochShuffling
|
|
262
|
+
nextShuffling: EpochShuffling;
|
|
267
263
|
nextActiveIndices: Uint32Array;
|
|
268
264
|
effectiveBalanceIncrements: EffectiveBalanceIncrements;
|
|
269
265
|
totalSlashingsByIncrement: number;
|
|
@@ -286,7 +282,6 @@ export class EpochCache {
|
|
|
286
282
|
this.config = data.config;
|
|
287
283
|
this.pubkey2index = data.pubkey2index;
|
|
288
284
|
this.index2pubkey = data.index2pubkey;
|
|
289
|
-
this.shufflingCache = data.shufflingCache;
|
|
290
285
|
this.proposers = data.proposers;
|
|
291
286
|
this.proposersPrevEpoch = data.proposersPrevEpoch;
|
|
292
287
|
this.proposersNextEpoch = data.proposersNextEpoch;
|
|
@@ -324,7 +319,7 @@ export class EpochCache {
|
|
|
324
319
|
*/
|
|
325
320
|
static createFromState(
|
|
326
321
|
state: BeaconStateAllForks,
|
|
327
|
-
{config, pubkey2index, index2pubkey
|
|
322
|
+
{config, pubkey2index, index2pubkey}: EpochCacheImmutableData,
|
|
328
323
|
opts?: EpochCacheOpts
|
|
329
324
|
): EpochCache {
|
|
330
325
|
const currentEpoch = computeEpochAtSlot(state.slot);
|
|
@@ -351,14 +346,15 @@ export class EpochCache {
|
|
|
351
346
|
const currentActiveIndicesAsNumberArray: ValidatorIndex[] = [];
|
|
352
347
|
const nextActiveIndicesAsNumberArray: ValidatorIndex[] = [];
|
|
353
348
|
|
|
354
|
-
// BeaconChain could provide a shuffling
|
|
349
|
+
// BeaconChain could provide a shuffling getter to avoid re-computing shuffling every epoch
|
|
355
350
|
// in that case, we don't need to compute shufflings again
|
|
351
|
+
const shufflingGetter = opts?.shufflingGetter;
|
|
356
352
|
const previousDecisionRoot = calculateShufflingDecisionRoot(config, state, previousEpoch);
|
|
357
|
-
const cachedPreviousShuffling =
|
|
353
|
+
const cachedPreviousShuffling = shufflingGetter?.(previousEpoch, previousDecisionRoot);
|
|
358
354
|
const currentDecisionRoot = calculateShufflingDecisionRoot(config, state, currentEpoch);
|
|
359
|
-
const cachedCurrentShuffling =
|
|
355
|
+
const cachedCurrentShuffling = shufflingGetter?.(currentEpoch, currentDecisionRoot);
|
|
360
356
|
const nextDecisionRoot = calculateShufflingDecisionRoot(config, state, nextEpoch);
|
|
361
|
-
const cachedNextShuffling =
|
|
357
|
+
const cachedNextShuffling = shufflingGetter?.(nextEpoch, nextDecisionRoot);
|
|
362
358
|
|
|
363
359
|
for (let i = 0; i < validatorCount; i++) {
|
|
364
360
|
const validator = validators[i];
|
|
@@ -366,8 +362,7 @@ export class EpochCache {
|
|
|
366
362
|
// Note: Not usable for fork-choice balances since in-active validators are not zero'ed
|
|
367
363
|
effectiveBalanceIncrements[i] = Math.floor(validator.effectiveBalance / EFFECTIVE_BALANCE_INCREMENT);
|
|
368
364
|
|
|
369
|
-
//
|
|
370
|
-
// skip doing that if we already have cached shufflings
|
|
365
|
+
// Collect active indices for each epoch to compute shufflings
|
|
371
366
|
if (cachedPreviousShuffling == null && isActiveValidator(validator, previousEpoch)) {
|
|
372
367
|
previousActiveIndicesAsNumberArray.push(i);
|
|
373
368
|
}
|
|
@@ -402,47 +397,19 @@ export class EpochCache {
|
|
|
402
397
|
}
|
|
403
398
|
|
|
404
399
|
const nextActiveIndices = new Uint32Array(nextActiveIndicesAsNumberArray);
|
|
405
|
-
let previousShuffling: EpochShuffling;
|
|
406
|
-
let currentShuffling: EpochShuffling;
|
|
407
|
-
let nextShuffling: EpochShuffling;
|
|
408
|
-
|
|
409
|
-
if (!shufflingCache) {
|
|
410
|
-
// Only for testing. shufflingCache should always be available in prod
|
|
411
|
-
previousShuffling = computeEpochShuffling(
|
|
412
|
-
state,
|
|
413
|
-
new Uint32Array(previousActiveIndicesAsNumberArray),
|
|
414
|
-
previousEpoch
|
|
415
|
-
);
|
|
416
400
|
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
401
|
+
// Use cached shufflings if available, otherwise compute
|
|
402
|
+
const currentShuffling =
|
|
403
|
+
cachedCurrentShuffling ??
|
|
404
|
+
computeEpochShuffling(state, new Uint32Array(currentActiveIndicesAsNumberArray), currentEpoch);
|
|
420
405
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
?
|
|
425
|
-
:
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
});
|
|
429
|
-
|
|
430
|
-
previousShuffling = cachedPreviousShuffling
|
|
431
|
-
? cachedPreviousShuffling
|
|
432
|
-
: isGenesis
|
|
433
|
-
? currentShuffling
|
|
434
|
-
: shufflingCache.getSync(previousEpoch, previousDecisionRoot, {
|
|
435
|
-
state,
|
|
436
|
-
activeIndices: new Uint32Array(previousActiveIndicesAsNumberArray),
|
|
437
|
-
});
|
|
438
|
-
|
|
439
|
-
nextShuffling = cachedNextShuffling
|
|
440
|
-
? cachedNextShuffling
|
|
441
|
-
: shufflingCache.getSync(nextEpoch, nextDecisionRoot, {
|
|
442
|
-
state,
|
|
443
|
-
activeIndices: nextActiveIndices,
|
|
444
|
-
});
|
|
445
|
-
}
|
|
406
|
+
const previousShuffling =
|
|
407
|
+
cachedPreviousShuffling ??
|
|
408
|
+
(isGenesis
|
|
409
|
+
? currentShuffling
|
|
410
|
+
: computeEpochShuffling(state, new Uint32Array(previousActiveIndicesAsNumberArray), previousEpoch));
|
|
411
|
+
|
|
412
|
+
const nextShuffling = cachedNextShuffling ?? computeEpochShuffling(state, nextActiveIndices, nextEpoch);
|
|
446
413
|
|
|
447
414
|
const currentProposerSeed = getSeed(state, currentEpoch, DOMAIN_BEACON_PROPOSER);
|
|
448
415
|
|
|
@@ -549,7 +516,6 @@ export class EpochCache {
|
|
|
549
516
|
config,
|
|
550
517
|
pubkey2index,
|
|
551
518
|
index2pubkey,
|
|
552
|
-
shufflingCache,
|
|
553
519
|
proposers,
|
|
554
520
|
// On first epoch, set to null to prevent unnecessary work since this is only used for metrics
|
|
555
521
|
proposersPrevEpoch: null,
|
|
@@ -593,7 +559,6 @@ export class EpochCache {
|
|
|
593
559
|
// Common append-only structures shared with all states, no need to clone
|
|
594
560
|
pubkey2index: this.pubkey2index,
|
|
595
561
|
index2pubkey: this.index2pubkey,
|
|
596
|
-
shufflingCache: this.shufflingCache,
|
|
597
562
|
// Immutable data
|
|
598
563
|
proposers: this.proposers,
|
|
599
564
|
proposersPrevEpoch: this.proposersPrevEpoch,
|
|
@@ -652,62 +617,26 @@ export class EpochCache {
|
|
|
652
617
|
this.previousShuffling = this.currentShuffling;
|
|
653
618
|
this.previousDecisionRoot = this.currentDecisionRoot;
|
|
654
619
|
|
|
655
|
-
// move next to current
|
|
620
|
+
// move next to current
|
|
656
621
|
this.currentDecisionRoot = this.nextDecisionRoot;
|
|
657
|
-
|
|
658
|
-
// was already pulled from the ShufflingCache to the EpochCache (should be in most cases)
|
|
659
|
-
this.currentShuffling = this.nextShuffling;
|
|
660
|
-
} else {
|
|
661
|
-
this.shufflingCache?.metrics?.shufflingCache.nextShufflingNotOnEpochCache.inc();
|
|
662
|
-
this.currentShuffling =
|
|
663
|
-
this.shufflingCache?.getSync(upcomingEpoch, this.currentDecisionRoot, {
|
|
664
|
-
state,
|
|
665
|
-
// have to use the "nextActiveIndices" that were saved in the last transition here to calculate
|
|
666
|
-
// the upcoming shuffling if it is not already built (similar condition to the below computation)
|
|
667
|
-
activeIndices: this.nextActiveIndices,
|
|
668
|
-
}) ??
|
|
669
|
-
// allow for this case during testing where the ShufflingCache is not present, may affect perf testing
|
|
670
|
-
// so should be taken into account when structuring tests. Should not affect unit or other tests though
|
|
671
|
-
computeEpochShuffling(state, this.nextActiveIndices, upcomingEpoch);
|
|
672
|
-
}
|
|
622
|
+
this.currentShuffling = this.nextShuffling;
|
|
673
623
|
|
|
674
|
-
//
|
|
675
|
-
|
|
624
|
+
// Compute shuffling for epoch n+2
|
|
625
|
+
//
|
|
626
|
+
// Post-Fulu (EIP-7917), the beacon state includes a `proposer_lookahead` field that stores
|
|
627
|
+
// proposer indices for MIN_SEED_LOOKAHEAD + 1 epochs ahead (2 epochs with MIN_SEED_LOOKAHEAD=1).
|
|
628
|
+
// At each epoch boundary, processProposerLookahead() shifts out the current epoch's proposers
|
|
629
|
+
// and appends new proposers for epoch n + MIN_SEED_LOOKAHEAD + 1 (i.e., epoch n+2).
|
|
630
|
+
//
|
|
631
|
+
// processProposerLookahead() already computes the n+2 shuffling and stores it in
|
|
632
|
+
// epochTransitionCache.nextShuffling. Reuse it here to avoid duplicate computation.
|
|
633
|
+
// Pre-Fulu, we need to compute it here since processProposerLookahead doesn't run.
|
|
634
|
+
//
|
|
635
|
+
// See: https://eips.ethereum.org/EIPS/eip-7917
|
|
636
|
+
this.nextDecisionRoot = calculateDecisionRoot(state, epochAfterUpcoming);
|
|
676
637
|
this.nextActiveIndices = epochTransitionCache.nextShufflingActiveIndices;
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
this.nextShuffling = this.shufflingCache.getSync(epochAfterUpcoming, this.nextDecisionRoot, {
|
|
680
|
-
state,
|
|
681
|
-
activeIndices: this.nextActiveIndices,
|
|
682
|
-
});
|
|
683
|
-
} else {
|
|
684
|
-
this.nextShuffling = null;
|
|
685
|
-
// This promise will resolve immediately after the synchronous code of the state-transition runs. Until
|
|
686
|
-
// the build is done on a worker thread it will be calculated immediately after the epoch transition
|
|
687
|
-
// completes. Once the work is done concurrently it should be ready by time this get runs so the promise
|
|
688
|
-
// will resolve directly on the next spin of the event loop because the epoch transition and shuffling take
|
|
689
|
-
// about the same time to calculate so theoretically its ready now. Do not await here though in case it
|
|
690
|
-
// is not ready yet as the transition must not be asynchronous.
|
|
691
|
-
this.shufflingCache
|
|
692
|
-
.get(epochAfterUpcoming, this.nextDecisionRoot)
|
|
693
|
-
.then((shuffling) => {
|
|
694
|
-
if (!shuffling) {
|
|
695
|
-
throw new Error("EpochShuffling not returned from get in afterProcessEpoch");
|
|
696
|
-
}
|
|
697
|
-
this.nextShuffling = shuffling;
|
|
698
|
-
})
|
|
699
|
-
.catch((err) => {
|
|
700
|
-
this.shufflingCache?.logger?.error(
|
|
701
|
-
"EPOCH_CONTEXT_SHUFFLING_BUILD_ERROR",
|
|
702
|
-
{epoch: epochAfterUpcoming, decisionRoot: epochTransitionCache.nextShufflingDecisionRoot},
|
|
703
|
-
err
|
|
704
|
-
);
|
|
705
|
-
});
|
|
706
|
-
}
|
|
707
|
-
} else {
|
|
708
|
-
// Only for testing. shufflingCache should always be available in prod
|
|
709
|
-
this.nextShuffling = computeEpochShuffling(state, this.nextActiveIndices, epochAfterUpcoming);
|
|
710
|
-
}
|
|
638
|
+
this.nextShuffling =
|
|
639
|
+
epochTransitionCache.nextShuffling ?? computeEpochShuffling(state, this.nextActiveIndices, epochAfterUpcoming);
|
|
711
640
|
|
|
712
641
|
// TODO: DEDUPLICATE from createEpochCache
|
|
713
642
|
//
|
|
@@ -826,22 +755,7 @@ export class EpochCache {
|
|
|
826
755
|
if (indices.length === 0) {
|
|
827
756
|
throw new Error("Attempt to get committees without providing CommitteeIndex");
|
|
828
757
|
}
|
|
829
|
-
|
|
830
|
-
const slotCommittees = this.getShufflingAtSlot(slot).committees[slot % SLOTS_PER_EPOCH];
|
|
831
|
-
const committees = [];
|
|
832
|
-
|
|
833
|
-
for (const index of indices) {
|
|
834
|
-
if (index >= slotCommittees.length) {
|
|
835
|
-
throw new EpochCacheError({
|
|
836
|
-
code: EpochCacheErrorCode.COMMITTEE_INDEX_OUT_OF_RANGE,
|
|
837
|
-
index,
|
|
838
|
-
maxIndex: slotCommittees.length,
|
|
839
|
-
});
|
|
840
|
-
}
|
|
841
|
-
committees.push(slotCommittees[index]);
|
|
842
|
-
}
|
|
843
|
-
|
|
844
|
-
return committees;
|
|
758
|
+
return getBeaconCommittees(this.getShufflingAtSlot(slot), slot, indices);
|
|
845
759
|
}
|
|
846
760
|
|
|
847
761
|
getCommitteeCountPerSlot(epoch: Epoch): number {
|
|
@@ -939,50 +853,16 @@ export class EpochCache {
|
|
|
939
853
|
* Return the indexed attestation corresponding to ``attestation``.
|
|
940
854
|
*/
|
|
941
855
|
getIndexedAttestation(fork: ForkSeq, attestation: Attestation): IndexedAttestation {
|
|
942
|
-
const
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
// sort in-place
|
|
946
|
-
attestingIndices.sort((a, b) => a - b);
|
|
947
|
-
return {
|
|
948
|
-
attestingIndices: attestingIndices,
|
|
949
|
-
data: data,
|
|
950
|
-
signature: attestation.signature,
|
|
951
|
-
};
|
|
856
|
+
const shuffling = this.getShufflingAtSlot(attestation.data.slot);
|
|
857
|
+
return getIndexedAttestation(shuffling, fork, attestation);
|
|
952
858
|
}
|
|
953
859
|
|
|
954
860
|
/**
|
|
955
861
|
* Return indices of validators who attestested in `attestation`
|
|
956
862
|
*/
|
|
957
863
|
getAttestingIndices(fork: ForkSeq, attestation: Attestation): number[] {
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
const validatorIndices = this.getBeaconCommittee(data.slot, data.index);
|
|
961
|
-
|
|
962
|
-
return aggregationBits.intersectValues(validatorIndices);
|
|
963
|
-
}
|
|
964
|
-
const {aggregationBits, committeeBits, data} = attestation as electra.Attestation;
|
|
965
|
-
|
|
966
|
-
// There is a naming conflict on the term `committeeIndices`
|
|
967
|
-
// In Lodestar it usually means a list of validator indices of participants in a committee
|
|
968
|
-
// In the spec it means a list of committee indices according to committeeBits
|
|
969
|
-
// This `committeeIndices` refers to the latter
|
|
970
|
-
// TODO Electra: resolve the naming conflicts
|
|
971
|
-
const committeeIndices = committeeBits.getTrueBitIndexes();
|
|
972
|
-
|
|
973
|
-
const validatorsByCommittee = this.getBeaconCommittees(data.slot, committeeIndices);
|
|
974
|
-
|
|
975
|
-
// Create a new Uint32Array to flatten `validatorsByCommittee`
|
|
976
|
-
const totalLength = validatorsByCommittee.reduce((acc, curr) => acc + curr.length, 0);
|
|
977
|
-
const committeeValidators = new Uint32Array(totalLength);
|
|
978
|
-
|
|
979
|
-
let offset = 0;
|
|
980
|
-
for (const committee of validatorsByCommittee) {
|
|
981
|
-
committeeValidators.set(committee, offset);
|
|
982
|
-
offset += committee.length;
|
|
983
|
-
}
|
|
984
|
-
|
|
985
|
-
return aggregationBits.intersectValues(committeeValidators);
|
|
864
|
+
const shuffling = this.getShufflingAtSlot(attestation.data.slot);
|
|
865
|
+
return getAttestingIndices(shuffling, fork, attestation);
|
|
986
866
|
}
|
|
987
867
|
|
|
988
868
|
getCommitteeAssignments(
|
|
@@ -993,38 +873,6 @@ export class EpochCache {
|
|
|
993
873
|
return calculateCommitteeAssignments(shuffling, requestedValidatorIndices);
|
|
994
874
|
}
|
|
995
875
|
|
|
996
|
-
/**
|
|
997
|
-
* Return the committee assignment in the ``epoch`` for ``validator_index``.
|
|
998
|
-
* ``assignment`` returned is a tuple of the following form:
|
|
999
|
-
* ``assignment[0]`` is the list of validators in the committee
|
|
1000
|
-
* ``assignment[1]`` is the index to which the committee is assigned
|
|
1001
|
-
* ``assignment[2]`` is the slot at which the committee is assigned
|
|
1002
|
-
* Return null if no assignment..
|
|
1003
|
-
*/
|
|
1004
|
-
getCommitteeAssignment(epoch: Epoch, validatorIndex: ValidatorIndex): phase0.CommitteeAssignment | null {
|
|
1005
|
-
if (epoch > this.currentShuffling.epoch + 1) {
|
|
1006
|
-
throw Error(
|
|
1007
|
-
`Requesting committee assignment for more than 1 epoch ahead: ${epoch} > ${this.currentShuffling.epoch} + 1`
|
|
1008
|
-
);
|
|
1009
|
-
}
|
|
1010
|
-
|
|
1011
|
-
const epochStartSlot = computeStartSlotAtEpoch(epoch);
|
|
1012
|
-
const committeeCountPerSlot = this.getCommitteeCountPerSlot(epoch);
|
|
1013
|
-
for (let slot = epochStartSlot; slot < epochStartSlot + SLOTS_PER_EPOCH; slot++) {
|
|
1014
|
-
for (let i = 0; i < committeeCountPerSlot; i++) {
|
|
1015
|
-
const committee = this.getBeaconCommittee(slot, i);
|
|
1016
|
-
if (committee.includes(validatorIndex)) {
|
|
1017
|
-
return {
|
|
1018
|
-
validators: Array.from(committee),
|
|
1019
|
-
committeeIndex: i,
|
|
1020
|
-
slot,
|
|
1021
|
-
};
|
|
1022
|
-
}
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
1025
|
-
return null;
|
|
1026
|
-
}
|
|
1027
|
-
|
|
1028
876
|
isAggregator(slot: Slot, index: CommitteeIndex, slotSignature: BLSSignature): boolean {
|
|
1029
877
|
const committee = this.getBeaconCommittee(slot, index);
|
|
1030
878
|
return isAggregatorFromCommitteeLength(committee.length, slotSignature);
|
|
@@ -1100,10 +948,6 @@ export class EpochCache {
|
|
|
1100
948
|
case this.epoch:
|
|
1101
949
|
return this.currentShuffling;
|
|
1102
950
|
case this.nextEpoch:
|
|
1103
|
-
if (!this.nextShuffling) {
|
|
1104
|
-
this.nextShuffling =
|
|
1105
|
-
this.shufflingCache?.getSync(this.nextEpoch, this.getShufflingDecisionRoot(this.nextEpoch)) ?? null;
|
|
1106
|
-
}
|
|
1107
951
|
return this.nextShuffling;
|
|
1108
952
|
default:
|
|
1109
953
|
return null;
|
|
@@ -1213,7 +1057,6 @@ function getEffectiveBalanceIncrementsByteLen(validatorCount: number): number {
|
|
|
1213
1057
|
}
|
|
1214
1058
|
|
|
1215
1059
|
export enum EpochCacheErrorCode {
|
|
1216
|
-
COMMITTEE_INDEX_OUT_OF_RANGE = "EPOCH_CONTEXT_ERROR_COMMITTEE_INDEX_OUT_OF_RANGE",
|
|
1217
1060
|
COMMITTEE_EPOCH_OUT_OF_RANGE = "EPOCH_CONTEXT_ERROR_COMMITTEE_EPOCH_OUT_OF_RANGE",
|
|
1218
1061
|
DECISION_ROOT_EPOCH_OUT_OF_RANGE = "EPOCH_CONTEXT_ERROR_DECISION_ROOT_EPOCH_OUT_OF_RANGE",
|
|
1219
1062
|
NEXT_SHUFFLING_NOT_AVAILABLE = "EPOCH_CONTEXT_ERROR_NEXT_SHUFFLING_NOT_AVAILABLE",
|
|
@@ -1222,11 +1065,6 @@ export enum EpochCacheErrorCode {
|
|
|
1222
1065
|
}
|
|
1223
1066
|
|
|
1224
1067
|
type EpochCacheErrorType =
|
|
1225
|
-
| {
|
|
1226
|
-
code: EpochCacheErrorCode.COMMITTEE_INDEX_OUT_OF_RANGE;
|
|
1227
|
-
index: number;
|
|
1228
|
-
maxIndex: number;
|
|
1229
|
-
}
|
|
1230
1068
|
| {
|
|
1231
1069
|
code: EpochCacheErrorCode.COMMITTEE_EPOCH_OUT_OF_RANGE;
|
|
1232
1070
|
requestedEpoch: Epoch;
|
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
ForkSeq,
|
|
5
|
-
MIN_ACTIVATION_BALANCE,
|
|
6
|
-
SLOTS_PER_HISTORICAL_ROOT,
|
|
7
|
-
} from "@lodestar/params";
|
|
8
|
-
import {Epoch, RootHex, ValidatorIndex} from "@lodestar/types";
|
|
9
|
-
import {intDiv, toRootHex} from "@lodestar/utils";
|
|
1
|
+
import {EPOCHS_PER_SLASHINGS_VECTOR, FAR_FUTURE_EPOCH, ForkSeq, MIN_ACTIVATION_BALANCE} from "@lodestar/params";
|
|
2
|
+
import {Epoch, ValidatorIndex} from "@lodestar/types";
|
|
3
|
+
import {intDiv} from "@lodestar/utils";
|
|
10
4
|
import {processPendingAttestations} from "../epoch/processPendingAttestations.js";
|
|
11
5
|
import {
|
|
12
6
|
CachedBeaconStateAllForks,
|
|
@@ -26,16 +20,13 @@ import {
|
|
|
26
20
|
FLAG_UNSLASHED,
|
|
27
21
|
hasMarkers,
|
|
28
22
|
} from "../util/attesterStatus.js";
|
|
23
|
+
import {EpochShuffling} from "../util/epochShuffling.js";
|
|
29
24
|
|
|
30
25
|
export type EpochTransitionCacheOpts = {
|
|
31
26
|
/**
|
|
32
27
|
* Assert progressive balances the same to EpochTransitionCache
|
|
33
28
|
*/
|
|
34
29
|
assertCorrectProgressiveBalances?: boolean;
|
|
35
|
-
/**
|
|
36
|
-
* Do not queue shuffling calculation async. Forces sync JIT calculation in afterProcessEpoch
|
|
37
|
-
*/
|
|
38
|
-
asyncShufflingCalculation?: boolean;
|
|
39
30
|
};
|
|
40
31
|
|
|
41
32
|
/**
|
|
@@ -162,9 +153,10 @@ export interface EpochTransitionCache {
|
|
|
162
153
|
nextShufflingActiveIndices: Uint32Array;
|
|
163
154
|
|
|
164
155
|
/**
|
|
165
|
-
*
|
|
156
|
+
* Pre-computed shuffling for epoch N+2, populated by processProposerLookahead (Fulu+).
|
|
157
|
+
* Used by afterProcessEpoch to avoid recomputing the same shuffling.
|
|
166
158
|
*/
|
|
167
|
-
|
|
159
|
+
nextShuffling: EpochShuffling | null;
|
|
168
160
|
|
|
169
161
|
/**
|
|
170
162
|
* Altair specific, this is total active balances for the next epoch.
|
|
@@ -179,12 +171,6 @@ export interface EpochTransitionCache {
|
|
|
179
171
|
*/
|
|
180
172
|
nextEpochTotalActiveBalanceByIncrement: number;
|
|
181
173
|
|
|
182
|
-
/**
|
|
183
|
-
* Compute the shuffling sync or async. Defaults to synchronous. Need to pass `true` with the
|
|
184
|
-
* `EpochTransitionCacheOpts`
|
|
185
|
-
*/
|
|
186
|
-
asyncShufflingCalculation: boolean;
|
|
187
|
-
|
|
188
174
|
/**
|
|
189
175
|
* Track by validator index if it's active in the prev epoch.
|
|
190
176
|
* Used in metrics
|
|
@@ -379,12 +365,7 @@ export function beforeProcessEpoch(
|
|
|
379
365
|
}
|
|
380
366
|
});
|
|
381
367
|
|
|
382
|
-
//
|
|
383
|
-
const epochAfterNext = state.epochCtx.nextEpoch + 1;
|
|
384
|
-
// cannot call calculateShufflingDecisionRoot here because spec prevent getting current slot
|
|
385
|
-
// as a decision block. we are part way through the transition though and this was added in
|
|
386
|
-
// process slot beforeProcessEpoch happens so it available and valid
|
|
387
|
-
const nextShufflingDecisionRoot = toRootHex(state.blockRoots.get(state.slot % SLOTS_PER_HISTORICAL_ROOT));
|
|
368
|
+
// Prepare shuffling data for epoch after next (nextShuffling post epoch transition)
|
|
388
369
|
const nextShufflingActiveIndices = new Uint32Array(nextEpochShufflingActiveIndicesLength);
|
|
389
370
|
if (nextEpochShufflingActiveIndicesLength > nextEpochShufflingActiveValidatorIndices.length) {
|
|
390
371
|
throw new Error(
|
|
@@ -396,11 +377,6 @@ export function beforeProcessEpoch(
|
|
|
396
377
|
nextShufflingActiveIndices[i] = nextEpochShufflingActiveValidatorIndices[i];
|
|
397
378
|
}
|
|
398
379
|
|
|
399
|
-
const asyncShufflingCalculation = opts?.asyncShufflingCalculation ?? false;
|
|
400
|
-
if (asyncShufflingCalculation) {
|
|
401
|
-
state.epochCtx.shufflingCache?.build(epochAfterNext, nextShufflingDecisionRoot, state, nextShufflingActiveIndices);
|
|
402
|
-
}
|
|
403
|
-
|
|
404
380
|
if (totalActiveStakeByIncrement < 1) {
|
|
405
381
|
totalActiveStakeByIncrement = 1;
|
|
406
382
|
} else if (totalActiveStakeByIncrement >= Number.MAX_SAFE_INTEGER) {
|
|
@@ -524,9 +500,8 @@ export function beforeProcessEpoch(
|
|
|
524
500
|
indicesEligibleForActivationQueue,
|
|
525
501
|
indicesEligibleForActivation: indicesEligibleForActivation.map(({validatorIndex}) => validatorIndex),
|
|
526
502
|
indicesToEject,
|
|
527
|
-
nextShufflingDecisionRoot,
|
|
528
503
|
nextShufflingActiveIndices,
|
|
529
|
-
|
|
504
|
+
nextShuffling: null,
|
|
530
505
|
// to be updated in processEffectiveBalanceUpdates
|
|
531
506
|
nextEpochTotalActiveBalanceByIncrement: 0,
|
|
532
507
|
isActivePrevEpoch,
|
package/src/cache/stateCache.ts
CHANGED
|
@@ -180,7 +180,7 @@ export function loadCachedBeaconState<T extends BeaconStateAllForks & BeaconStat
|
|
|
180
180
|
stateBytes,
|
|
181
181
|
seedValidatorsBytes
|
|
182
182
|
);
|
|
183
|
-
const {pubkey2index, index2pubkey
|
|
183
|
+
const {pubkey2index, index2pubkey} = cachedSeedState.epochCtx;
|
|
184
184
|
// Get the validators sub tree once for all the loop
|
|
185
185
|
const validators = migratedState.validators;
|
|
186
186
|
for (const validatorIndex of modifiedValidators) {
|
|
@@ -196,7 +196,6 @@ export function loadCachedBeaconState<T extends BeaconStateAllForks & BeaconStat
|
|
|
196
196
|
config: cachedSeedState.config,
|
|
197
197
|
pubkey2index,
|
|
198
198
|
index2pubkey,
|
|
199
|
-
shufflingCache,
|
|
200
199
|
},
|
|
201
200
|
{...(opts ?? {}), ...{skipSyncPubkeys: true}}
|
|
202
201
|
) as T;
|
|
@@ -22,13 +22,9 @@ export function processProposerLookahead(
|
|
|
22
22
|
// Fill in the last epoch with new proposer indices
|
|
23
23
|
const epoch = state.epochCtx.epoch + MIN_SEED_LOOKAHEAD + 1;
|
|
24
24
|
|
|
25
|
-
const shuffling =
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
activeIndices: cache.nextShufflingActiveIndices,
|
|
29
|
-
}) ??
|
|
30
|
-
// Only for testing. shufflingCache should always be available in prod
|
|
31
|
-
computeEpochShuffling(state, cache.nextShufflingActiveIndices, epoch);
|
|
25
|
+
const shuffling = computeEpochShuffling(state, cache.nextShufflingActiveIndices, epoch);
|
|
26
|
+
// Save shuffling to cache so afterProcessEpoch can reuse it instead of recomputing
|
|
27
|
+
cache.nextShuffling = shuffling;
|
|
32
28
|
|
|
33
29
|
const lastEpochProposerLookahead = computeProposerIndices(fork, state, shuffling, epoch);
|
|
34
30
|
|
package/src/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
} from "@lodestar/params";
|
|
8
8
|
import {BeaconBlock, altair, phase0, rewards} from "@lodestar/types";
|
|
9
9
|
import {processAttestationsAltair} from "../block/processAttestationsAltair.js";
|
|
10
|
+
import {RewardCache} from "../cache/rewardCache.js";
|
|
10
11
|
import {CachedBeaconStateAllForks, CachedBeaconStateAltair, CachedBeaconStatePhase0} from "../cache/stateCache.js";
|
|
11
12
|
import {getAttesterSlashableIndices} from "../util/attestation.js";
|
|
12
13
|
|
|
@@ -23,12 +24,14 @@ type SubRewardValue = number; // All reward values should be integer
|
|
|
23
24
|
export async function computeBlockRewards(
|
|
24
25
|
config: BeaconConfig,
|
|
25
26
|
block: BeaconBlock,
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
preStateIn: CachedBeaconStateAllForks,
|
|
28
|
+
proposerRewards?: RewardCache
|
|
28
29
|
): Promise<rewards.BlockRewards> {
|
|
30
|
+
const preState = preStateIn.clone();
|
|
31
|
+
|
|
29
32
|
const fork = config.getForkName(block.slot);
|
|
30
33
|
const {attestations: cachedAttestationsReward = 0, syncAggregate: cachedSyncAggregateReward = 0} =
|
|
31
|
-
|
|
34
|
+
proposerRewards ?? {};
|
|
32
35
|
let blockAttestationReward = cachedAttestationsReward;
|
|
33
36
|
let syncAggregateReward = cachedSyncAggregateReward;
|
|
34
37
|
|
|
@@ -4,8 +4,6 @@ import {BeaconBlock, ValidatorIndex, altair, rewards} from "@lodestar/types";
|
|
|
4
4
|
import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
|
|
5
5
|
import {CachedBeaconStateAllForks, CachedBeaconStateAltair} from "../cache/stateCache.js";
|
|
6
6
|
|
|
7
|
-
type BalanceRecord = {val: number}; // Use val for convenient way to increment/decrement balance
|
|
8
|
-
|
|
9
7
|
export async function computeSyncCommitteeRewards(
|
|
10
8
|
config: BeaconConfig,
|
|
11
9
|
index2pubkey: Index2PubkeyCache,
|
|
@@ -19,7 +17,7 @@ export async function computeSyncCommitteeRewards(
|
|
|
19
17
|
}
|
|
20
18
|
|
|
21
19
|
const altairBlock = block as altair.BeaconBlock;
|
|
22
|
-
const preStateAltair = preState as CachedBeaconStateAltair;
|
|
20
|
+
const preStateAltair = preState.clone() as CachedBeaconStateAltair;
|
|
23
21
|
|
|
24
22
|
// Bound syncCommitteeValidatorIndices in case it goes beyond SYNC_COMMITTEE_SIZE just to be safe
|
|
25
23
|
const syncCommitteeValidatorIndices = preStateAltair.epochCtx.currentSyncCommitteeIndexed.validatorIndices.slice(
|
|
@@ -29,24 +27,23 @@ export async function computeSyncCommitteeRewards(
|
|
|
29
27
|
const {syncParticipantReward} = preStateAltair.epochCtx;
|
|
30
28
|
const {syncCommitteeBits} = altairBlock.body.syncAggregate;
|
|
31
29
|
|
|
32
|
-
//
|
|
33
|
-
const
|
|
34
|
-
for (const i of syncCommitteeValidatorIndices) {
|
|
35
|
-
balances.set(i, {val: preStateAltair.balances.get(i)});
|
|
36
|
-
}
|
|
30
|
+
// Track reward deltas per validator (can appear multiple times in sync committee)
|
|
31
|
+
const rewardDeltas: Map<ValidatorIndex, number> = new Map();
|
|
37
32
|
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
// Iterate by position index to correctly access syncCommitteeBits
|
|
34
|
+
for (let i = 0; i < syncCommitteeValidatorIndices.length; i++) {
|
|
35
|
+
const validatorIndex = syncCommitteeValidatorIndices[i];
|
|
36
|
+
const currentDelta = rewardDeltas.get(validatorIndex) ?? 0;
|
|
40
37
|
if (syncCommitteeBits.get(i)) {
|
|
41
38
|
// Positive rewards for participants
|
|
42
|
-
|
|
39
|
+
rewardDeltas.set(validatorIndex, currentDelta + syncParticipantReward);
|
|
43
40
|
} else {
|
|
44
41
|
// Negative rewards for non participants
|
|
45
|
-
|
|
42
|
+
rewardDeltas.set(validatorIndex, currentDelta - syncParticipantReward);
|
|
46
43
|
}
|
|
47
44
|
}
|
|
48
45
|
|
|
49
|
-
const rewards = Array.from(
|
|
46
|
+
const rewards = Array.from(rewardDeltas, ([validatorIndex, reward]) => ({validatorIndex, reward}));
|
|
50
47
|
|
|
51
48
|
if (validatorIds.length) {
|
|
52
49
|
const filtersSet = new Set(validatorIds);
|