@lodestar/state-transition 1.39.0-dev.87ff5db949 → 1.39.0-dev.90334173dc
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/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/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/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/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
|
@@ -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);
|
package/src/types.ts
CHANGED
|
@@ -9,54 +9,13 @@ import {
|
|
|
9
9
|
TARGET_COMMITTEE_SIZE,
|
|
10
10
|
} from "@lodestar/params";
|
|
11
11
|
import {Epoch, RootHex, ValidatorIndex, ssz} from "@lodestar/types";
|
|
12
|
-
import {
|
|
12
|
+
import {intDiv, toRootHex} from "@lodestar/utils";
|
|
13
13
|
import {BeaconStateAllForks} from "../types.js";
|
|
14
14
|
import {getBlockRootAtSlot} from "./blockRoot.js";
|
|
15
15
|
import {computeAnchorCheckpoint} from "./computeAnchorCheckpoint.js";
|
|
16
16
|
import {computeStartSlotAtEpoch} from "./epoch.js";
|
|
17
17
|
import {getSeed} from "./seed.js";
|
|
18
18
|
|
|
19
|
-
export interface ShufflingBuildProps {
|
|
20
|
-
state: BeaconStateAllForks;
|
|
21
|
-
activeIndices: Uint32Array;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface PublicShufflingCacheMetrics {
|
|
25
|
-
shufflingCache: {
|
|
26
|
-
nextShufflingNotOnEpochCache: GaugeExtra<NoLabels>;
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
export interface IShufflingCache {
|
|
30
|
-
metrics: PublicShufflingCacheMetrics | null;
|
|
31
|
-
logger: Logger | null;
|
|
32
|
-
/**
|
|
33
|
-
* Gets a cached shuffling via the epoch and decision root. If the state and
|
|
34
|
-
* activeIndices are passed and a shuffling is not available it will be built
|
|
35
|
-
* synchronously. If the state is not passed and the shuffling is not available
|
|
36
|
-
* nothing will be returned.
|
|
37
|
-
*
|
|
38
|
-
* NOTE: If a shuffling is already queued and not calculated it will build and resolve
|
|
39
|
-
* the promise but the already queued build will happen at some later time
|
|
40
|
-
*/
|
|
41
|
-
getSync<T extends ShufflingBuildProps | undefined>(
|
|
42
|
-
epoch: Epoch,
|
|
43
|
-
decisionRoot: RootHex,
|
|
44
|
-
buildProps?: T
|
|
45
|
-
): T extends ShufflingBuildProps ? EpochShuffling : EpochShuffling | null;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Gets a cached shuffling via the epoch and decision root. Returns a promise
|
|
49
|
-
* for the shuffling if it hs not calculated yet. Returns null if a build has
|
|
50
|
-
* not been queued nor a shuffling was calculated.
|
|
51
|
-
*/
|
|
52
|
-
get(epoch: Epoch, decisionRoot: RootHex): Promise<EpochShuffling | null>;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Queue asynchronous build for an EpochShuffling
|
|
56
|
-
*/
|
|
57
|
-
build(epoch: Epoch, decisionRoot: RootHex, state: BeaconStateAllForks, activeIndices: Uint32Array): void;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
19
|
/**
|
|
61
20
|
* Readonly interface for EpochShuffling.
|
|
62
21
|
*/
|
|
@@ -164,7 +123,7 @@ export async function computeEpochShufflingAsync(
|
|
|
164
123
|
};
|
|
165
124
|
}
|
|
166
125
|
|
|
167
|
-
function calculateDecisionRoot(state: BeaconStateAllForks, epoch: Epoch): RootHex {
|
|
126
|
+
export function calculateDecisionRoot(state: BeaconStateAllForks, epoch: Epoch): RootHex {
|
|
168
127
|
const pivotSlot = computeStartSlotAtEpoch(epoch - 1) - 1;
|
|
169
128
|
return toRootHex(getBlockRootAtSlot(state, pivotSlot));
|
|
170
129
|
}
|
package/src/util/index.ts
CHANGED
|
@@ -5,7 +5,6 @@ export * from "./attesterStatus.js";
|
|
|
5
5
|
export * from "./balance.js";
|
|
6
6
|
export * from "./blindedBlock.js";
|
|
7
7
|
export * from "./blockRoot.js";
|
|
8
|
-
export * from "./calculateCommitteeAssignments.js";
|
|
9
8
|
export * from "./capella.js";
|
|
10
9
|
export * from "./computeAnchorCheckpoint.js";
|
|
11
10
|
export * from "./deposit.js";
|
|
@@ -20,7 +19,7 @@ export * from "./interop.js";
|
|
|
20
19
|
export * from "./loadState/index.js";
|
|
21
20
|
export * from "./rootCache.js";
|
|
22
21
|
export * from "./seed.js";
|
|
23
|
-
export * from "./
|
|
22
|
+
export * from "./shuffling.js";
|
|
24
23
|
export * from "./signatureSets.js";
|
|
25
24
|
export * from "./signingRoot.js";
|
|
26
25
|
export * from "./slot.js";
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import {ForkName, ForkSeq, SLOTS_PER_EPOCH, isForkPostFulu} from "@lodestar/params";
|
|
2
|
+
import {
|
|
3
|
+
Attestation,
|
|
4
|
+
CommitteeIndex,
|
|
5
|
+
Epoch,
|
|
6
|
+
IndexedAttestation,
|
|
7
|
+
Root,
|
|
8
|
+
Slot,
|
|
9
|
+
ValidatorIndex,
|
|
10
|
+
electra,
|
|
11
|
+
} from "@lodestar/types";
|
|
12
|
+
import {LodestarError} from "@lodestar/utils";
|
|
13
|
+
import {CachedBeaconStateAllForks} from "../cache/stateCache.js";
|
|
14
|
+
import {getBlockRootAtSlot} from "./blockRoot.js";
|
|
15
|
+
import {computeStartSlotAtEpoch} from "./epoch.js";
|
|
16
|
+
import {EpochShuffling} from "./epochShuffling.js";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Returns the block root which decided the proposer shuffling for the current epoch. This root
|
|
20
|
+
* can be used to key this proposer shuffling.
|
|
21
|
+
*
|
|
22
|
+
* Returns `null` on the one-off scenario where the genesis block decides its own shuffling.
|
|
23
|
+
* It should be set to the latest block applied to this `state` or the genesis block root.
|
|
24
|
+
*/
|
|
25
|
+
export function proposerShufflingDecisionRoot(fork: ForkName, state: CachedBeaconStateAllForks): Root | null {
|
|
26
|
+
const decisionSlot = proposerShufflingDecisionSlot(fork, state);
|
|
27
|
+
if (state.slot === decisionSlot) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
return getBlockRootAtSlot(state, decisionSlot);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Returns the slot at which the proposer shuffling was decided. The block root at this slot
|
|
35
|
+
* can be used to key the proposer shuffling for the current epoch.
|
|
36
|
+
*/
|
|
37
|
+
function proposerShufflingDecisionSlot(fork: ForkName, state: CachedBeaconStateAllForks): Slot {
|
|
38
|
+
// After fulu, the decision slot is in previous epoch due to deterministic proposer lookahead
|
|
39
|
+
const epoch = isForkPostFulu(fork) ? state.epochCtx.epoch - 1 : state.epochCtx.epoch;
|
|
40
|
+
const startSlot = computeStartSlotAtEpoch(epoch);
|
|
41
|
+
return Math.max(startSlot - 1, 0);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Returns the block root which decided the attester shuffling for the given `requestedEpoch`.
|
|
46
|
+
* This root can be used to key that attester shuffling.
|
|
47
|
+
*
|
|
48
|
+
* Returns `null` on the one-off scenario where the genesis block decides its own shuffling.
|
|
49
|
+
* It should be set to the latest block applied to this `state` or the genesis block root.
|
|
50
|
+
*/
|
|
51
|
+
export function attesterShufflingDecisionRoot(state: CachedBeaconStateAllForks, requestedEpoch: Epoch): Root | null {
|
|
52
|
+
const decisionSlot = attesterShufflingDecisionSlot(state, requestedEpoch);
|
|
53
|
+
if (state.slot === decisionSlot) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
return getBlockRootAtSlot(state, decisionSlot);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Returns the slot at which the proposer shuffling was decided. The block root at this slot
|
|
61
|
+
* can be used to key the proposer shuffling for the current epoch.
|
|
62
|
+
*/
|
|
63
|
+
function attesterShufflingDecisionSlot(state: CachedBeaconStateAllForks, requestedEpoch: Epoch): Slot {
|
|
64
|
+
const epoch = attesterShufflingDecisionEpoch(state, requestedEpoch);
|
|
65
|
+
const slot = computeStartSlotAtEpoch(epoch);
|
|
66
|
+
return Math.max(slot - 1, 0);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Returns the epoch at which the attester shuffling was decided.
|
|
71
|
+
*
|
|
72
|
+
* Spec ref: https://github.com/ethereum/beacon-APIs/blob/v2.1.0/apis/validator/duties/attester.yaml#L15
|
|
73
|
+
*
|
|
74
|
+
* Throws an error when:
|
|
75
|
+
* - `EpochTooLow` when `requestedEpoch` is more than 1 prior to `currentEpoch`.
|
|
76
|
+
* - `EpochTooHigh` when `requestedEpoch` is more than 1 after `currentEpoch`.
|
|
77
|
+
*/
|
|
78
|
+
function attesterShufflingDecisionEpoch(state: CachedBeaconStateAllForks, requestedEpoch: Epoch): Epoch {
|
|
79
|
+
const currentEpoch = state.epochCtx.epoch;
|
|
80
|
+
|
|
81
|
+
// Next
|
|
82
|
+
if (requestedEpoch === currentEpoch + 1) return currentEpoch;
|
|
83
|
+
// Current
|
|
84
|
+
if (requestedEpoch === currentEpoch) return Math.max(currentEpoch - 1, 0);
|
|
85
|
+
// Previous
|
|
86
|
+
if (requestedEpoch === currentEpoch - 1) return Math.max(currentEpoch - 2, 0);
|
|
87
|
+
|
|
88
|
+
if (requestedEpoch < currentEpoch) {
|
|
89
|
+
throw Error(`EpochTooLow: current ${currentEpoch} requested ${requestedEpoch}`);
|
|
90
|
+
}
|
|
91
|
+
throw Error(`EpochTooHigh: current ${currentEpoch} requested ${requestedEpoch}`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Copied from lodestar-api package to avoid depending on the package
|
|
95
|
+
export interface AttesterDuty {
|
|
96
|
+
validatorIndex: ValidatorIndex;
|
|
97
|
+
committeeIndex: CommitteeIndex;
|
|
98
|
+
committeeLength: number;
|
|
99
|
+
committeesAtSlot: number;
|
|
100
|
+
validatorCommitteeIndex: number;
|
|
101
|
+
slot: Slot;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function calculateCommitteeAssignments(
|
|
105
|
+
epochShuffling: EpochShuffling,
|
|
106
|
+
requestedValidatorIndices: ValidatorIndex[]
|
|
107
|
+
): Map<ValidatorIndex, AttesterDuty> {
|
|
108
|
+
const requestedValidatorIndicesSet = new Set(requestedValidatorIndices);
|
|
109
|
+
const duties = new Map<ValidatorIndex, AttesterDuty>();
|
|
110
|
+
|
|
111
|
+
const epochCommittees = epochShuffling.committees;
|
|
112
|
+
for (let epochSlot = 0; epochSlot < SLOTS_PER_EPOCH; epochSlot++) {
|
|
113
|
+
const slotCommittees = epochCommittees[epochSlot];
|
|
114
|
+
for (let i = 0, committeesAtSlot = slotCommittees.length; i < committeesAtSlot; i++) {
|
|
115
|
+
for (let j = 0, committeeLength = slotCommittees[i].length; j < committeeLength; j++) {
|
|
116
|
+
const validatorIndex = slotCommittees[i][j];
|
|
117
|
+
if (requestedValidatorIndicesSet.has(validatorIndex)) {
|
|
118
|
+
duties.set(validatorIndex, {
|
|
119
|
+
validatorIndex,
|
|
120
|
+
committeeLength,
|
|
121
|
+
committeesAtSlot,
|
|
122
|
+
validatorCommitteeIndex: j,
|
|
123
|
+
committeeIndex: i,
|
|
124
|
+
slot: epochShuffling.epoch * SLOTS_PER_EPOCH + epochSlot,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return duties;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Return the indexed attestation corresponding to ``attestation``.
|
|
136
|
+
*/
|
|
137
|
+
export function getIndexedAttestation(
|
|
138
|
+
epochShuffling: EpochShuffling,
|
|
139
|
+
fork: ForkSeq,
|
|
140
|
+
attestation: Attestation
|
|
141
|
+
): IndexedAttestation {
|
|
142
|
+
const {data} = attestation;
|
|
143
|
+
const attestingIndices = getAttestingIndices(epochShuffling, fork, attestation);
|
|
144
|
+
|
|
145
|
+
// sort in-place
|
|
146
|
+
attestingIndices.sort((a, b) => a - b);
|
|
147
|
+
return {
|
|
148
|
+
attestingIndices: attestingIndices,
|
|
149
|
+
data: data,
|
|
150
|
+
signature: attestation.signature,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Return indices of validators who attestested in `attestation`
|
|
156
|
+
*/
|
|
157
|
+
export function getAttestingIndices(epochShuffling: EpochShuffling, fork: ForkSeq, attestation: Attestation): number[] {
|
|
158
|
+
if (fork < ForkSeq.electra) {
|
|
159
|
+
const {aggregationBits, data} = attestation;
|
|
160
|
+
const validatorIndices = getBeaconCommittee(epochShuffling, data.slot, data.index);
|
|
161
|
+
|
|
162
|
+
return aggregationBits.intersectValues(validatorIndices);
|
|
163
|
+
}
|
|
164
|
+
const {aggregationBits, committeeBits, data} = attestation as electra.Attestation;
|
|
165
|
+
|
|
166
|
+
// There is a naming conflict on the term `committeeIndices`
|
|
167
|
+
// In Lodestar it usually means a list of validator indices of participants in a committee
|
|
168
|
+
// In the spec it means a list of committee indices according to committeeBits
|
|
169
|
+
// This `committeeIndices` refers to the latter
|
|
170
|
+
// TODO Electra: resolve the naming conflicts
|
|
171
|
+
const committeeIndices = committeeBits.getTrueBitIndexes();
|
|
172
|
+
|
|
173
|
+
const validatorsByCommittee = getBeaconCommittees(epochShuffling, data.slot, committeeIndices);
|
|
174
|
+
|
|
175
|
+
// Create a new Uint32Array to flatten `validatorsByCommittee`
|
|
176
|
+
const totalLength = validatorsByCommittee.reduce((acc, curr) => acc + curr.length, 0);
|
|
177
|
+
const committeeValidators = new Uint32Array(totalLength);
|
|
178
|
+
|
|
179
|
+
let offset = 0;
|
|
180
|
+
for (const committee of validatorsByCommittee) {
|
|
181
|
+
committeeValidators.set(committee, offset);
|
|
182
|
+
offset += committee.length;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return aggregationBits.intersectValues(committeeValidators);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Return the beacon committee at slot for index.
|
|
190
|
+
*/
|
|
191
|
+
export function getBeaconCommittee(epochShuffling: EpochShuffling, slot: Slot, index: CommitteeIndex): Uint32Array {
|
|
192
|
+
return getBeaconCommittees(epochShuffling, slot, [index])[0];
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Return a Uint32Array[] representing committees validator indices
|
|
197
|
+
*/
|
|
198
|
+
export function getBeaconCommittees(
|
|
199
|
+
epochShuffling: EpochShuffling,
|
|
200
|
+
slot: Slot,
|
|
201
|
+
indices: CommitteeIndex[]
|
|
202
|
+
): Uint32Array[] {
|
|
203
|
+
if (indices.length === 0) {
|
|
204
|
+
throw new Error("Attempt to get committees without providing CommitteeIndex");
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const slotCommittees = epochShuffling.committees[slot % SLOTS_PER_EPOCH];
|
|
208
|
+
const committees = [];
|
|
209
|
+
|
|
210
|
+
for (const index of indices) {
|
|
211
|
+
if (index >= slotCommittees.length) {
|
|
212
|
+
throw new ShufflingError({
|
|
213
|
+
code: ShufflingErrorCode.COMMITTEE_INDEX_OUT_OF_RANGE,
|
|
214
|
+
index,
|
|
215
|
+
maxIndex: slotCommittees.length,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
committees.push(slotCommittees[index]);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return committees;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export enum ShufflingErrorCode {
|
|
225
|
+
COMMITTEE_INDEX_OUT_OF_RANGE = "SHUFFLING_ERROR_COMMITTEE_INDEX_OUT_OF_RANGE",
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
type ShufflingErrorType = {
|
|
229
|
+
code: ShufflingErrorCode.COMMITTEE_INDEX_OUT_OF_RANGE;
|
|
230
|
+
index: number;
|
|
231
|
+
maxIndex: number;
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
export class ShufflingError extends LodestarError<ShufflingErrorType> {}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { CommitteeIndex, Slot, ValidatorIndex } from "@lodestar/types";
|
|
2
|
-
import { EpochShuffling } from "./epochShuffling.js";
|
|
3
|
-
export interface AttesterDuty {
|
|
4
|
-
validatorIndex: ValidatorIndex;
|
|
5
|
-
committeeIndex: CommitteeIndex;
|
|
6
|
-
committeeLength: number;
|
|
7
|
-
committeesAtSlot: number;
|
|
8
|
-
validatorCommitteeIndex: number;
|
|
9
|
-
slot: Slot;
|
|
10
|
-
}
|
|
11
|
-
export declare function calculateCommitteeAssignments(epochShuffling: EpochShuffling, requestedValidatorIndices: ValidatorIndex[]): Map<ValidatorIndex, AttesterDuty>;
|
|
12
|
-
//# sourceMappingURL=calculateCommitteeAssignments.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"calculateCommitteeAssignments.d.ts","sourceRoot":"","sources":["../../src/util/calculateCommitteeAssignments.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,cAAc,EAAE,IAAI,EAAE,cAAc,EAAC,MAAM,iBAAiB,CAAC;AACrE,OAAO,EAAC,cAAc,EAAC,MAAM,qBAAqB,CAAC;AAGnD,MAAM,WAAW,YAAY;IAC3B,cAAc,EAAE,cAAc,CAAC;IAC/B,cAAc,EAAE,cAAc,CAAC;IAC/B,eAAe,EAAE,MAAM,CAAC;IACxB,gBAAgB,EAAE,MAAM,CAAC;IACzB,uBAAuB,EAAE,MAAM,CAAC;IAChC,IAAI,EAAE,IAAI,CAAC;CACZ;AAED,wBAAgB,6BAA6B,CAC3C,cAAc,EAAE,cAAc,EAC9B,yBAAyB,EAAE,cAAc,EAAE,GAC1C,GAAG,CAAC,cAAc,EAAE,YAAY,CAAC,CAyBnC"}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { SLOTS_PER_EPOCH } from "@lodestar/params";
|
|
2
|
-
export function calculateCommitteeAssignments(epochShuffling, requestedValidatorIndices) {
|
|
3
|
-
const requestedValidatorIndicesSet = new Set(requestedValidatorIndices);
|
|
4
|
-
const duties = new Map();
|
|
5
|
-
const epochCommittees = epochShuffling.committees;
|
|
6
|
-
for (let epochSlot = 0; epochSlot < SLOTS_PER_EPOCH; epochSlot++) {
|
|
7
|
-
const slotCommittees = epochCommittees[epochSlot];
|
|
8
|
-
for (let i = 0, committeesAtSlot = slotCommittees.length; i < committeesAtSlot; i++) {
|
|
9
|
-
for (let j = 0, committeeLength = slotCommittees[i].length; j < committeeLength; j++) {
|
|
10
|
-
const validatorIndex = slotCommittees[i][j];
|
|
11
|
-
if (requestedValidatorIndicesSet.has(validatorIndex)) {
|
|
12
|
-
duties.set(validatorIndex, {
|
|
13
|
-
validatorIndex,
|
|
14
|
-
committeeLength,
|
|
15
|
-
committeesAtSlot,
|
|
16
|
-
validatorCommitteeIndex: j,
|
|
17
|
-
committeeIndex: i,
|
|
18
|
-
slot: epochShuffling.epoch * SLOTS_PER_EPOCH + epochSlot,
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return duties;
|
|
25
|
-
}
|
|
26
|
-
//# sourceMappingURL=calculateCommitteeAssignments.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"calculateCommitteeAssignments.js","sourceRoot":"","sources":["../../src/util/calculateCommitteeAssignments.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAC,MAAM,kBAAkB,CAAC;AAcjD,MAAM,UAAU,6BAA6B,CAC3C,cAA8B,EAC9B,yBAA2C;IAE3C,MAAM,4BAA4B,GAAG,IAAI,GAAG,CAAC,yBAAyB,CAAC,CAAC;IACxE,MAAM,MAAM,GAAG,IAAI,GAAG,EAAgC,CAAC;IAEvD,MAAM,eAAe,GAAG,cAAc,CAAC,UAAU,CAAC;IAClD,KAAK,IAAI,SAAS,GAAG,CAAC,EAAE,SAAS,GAAG,eAAe,EAAE,SAAS,EAAE,EAAE,CAAC;QACjE,MAAM,cAAc,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;QAClD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,gBAAgB,GAAG,cAAc,CAAC,MAAM,EAAE,CAAC,GAAG,gBAAgB,EAAE,CAAC,EAAE,EAAE,CAAC;YACpF,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,eAAe,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,eAAe,EAAE,CAAC,EAAE,EAAE,CAAC;gBACrF,MAAM,cAAc,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;gBAC5C,IAAI,4BAA4B,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,CAAC;oBACrD,MAAM,CAAC,GAAG,CAAC,cAAc,EAAE;wBACzB,cAAc;wBACd,eAAe;wBACf,gBAAgB;wBAChB,uBAAuB,EAAE,CAAC;wBAC1B,cAAc,EAAE,CAAC;wBACjB,IAAI,EAAE,cAAc,CAAC,KAAK,GAAG,eAAe,GAAG,SAAS;qBACzD,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { ForkName } from "@lodestar/params";
|
|
2
|
-
import { Epoch, Root } from "@lodestar/types";
|
|
3
|
-
import { CachedBeaconStateAllForks } from "../types.js";
|
|
4
|
-
/**
|
|
5
|
-
* Returns the block root which decided the proposer shuffling for the current epoch. This root
|
|
6
|
-
* can be used to key this proposer shuffling.
|
|
7
|
-
*
|
|
8
|
-
* Returns `null` on the one-off scenario where the genesis block decides its own shuffling.
|
|
9
|
-
* It should be set to the latest block applied to this `state` or the genesis block root.
|
|
10
|
-
*/
|
|
11
|
-
export declare function proposerShufflingDecisionRoot(fork: ForkName, state: CachedBeaconStateAllForks): Root | null;
|
|
12
|
-
/**
|
|
13
|
-
* Returns the block root which decided the attester shuffling for the given `requestedEpoch`.
|
|
14
|
-
* This root can be used to key that attester shuffling.
|
|
15
|
-
*
|
|
16
|
-
* Returns `null` on the one-off scenario where the genesis block decides its own shuffling.
|
|
17
|
-
* It should be set to the latest block applied to this `state` or the genesis block root.
|
|
18
|
-
*/
|
|
19
|
-
export declare function attesterShufflingDecisionRoot(state: CachedBeaconStateAllForks, requestedEpoch: Epoch): Root | null;
|
|
20
|
-
//# sourceMappingURL=shufflingDecisionRoot.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"shufflingDecisionRoot.d.ts","sourceRoot":"","sources":["../../src/util/shufflingDecisionRoot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAiB,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAC,KAAK,EAAE,IAAI,EAAO,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAC,yBAAyB,EAAC,MAAM,aAAa,CAAC;AAItD;;;;;;GAMG;AACH,wBAAgB,6BAA6B,CAAC,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,yBAAyB,GAAG,IAAI,GAAG,IAAI,CAM3G;AAaD;;;;;;GAMG;AACH,wBAAgB,6BAA6B,CAAC,KAAK,EAAE,yBAAyB,EAAE,cAAc,EAAE,KAAK,GAAG,IAAI,GAAG,IAAI,CAMlH"}
|