@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
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { ForkSeq, SLOTS_PER_EPOCH, isForkPostFulu } from "@lodestar/params";
|
|
2
|
+
import { LodestarError } from "@lodestar/utils";
|
|
3
|
+
import { getBlockRootAtSlot } from "./blockRoot.js";
|
|
4
|
+
import { computeStartSlotAtEpoch } from "./epoch.js";
|
|
5
|
+
/**
|
|
6
|
+
* Returns the block root which decided the proposer shuffling for the current epoch. This root
|
|
7
|
+
* can be used to key this proposer shuffling.
|
|
8
|
+
*
|
|
9
|
+
* Returns `null` on the one-off scenario where the genesis block decides its own shuffling.
|
|
10
|
+
* It should be set to the latest block applied to this `state` or the genesis block root.
|
|
11
|
+
*/
|
|
12
|
+
export function proposerShufflingDecisionRoot(fork, state) {
|
|
13
|
+
const decisionSlot = proposerShufflingDecisionSlot(fork, state);
|
|
14
|
+
if (state.slot === decisionSlot) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
return getBlockRootAtSlot(state, decisionSlot);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Returns the slot at which the proposer shuffling was decided. The block root at this slot
|
|
21
|
+
* can be used to key the proposer shuffling for the current epoch.
|
|
22
|
+
*/
|
|
23
|
+
function proposerShufflingDecisionSlot(fork, state) {
|
|
24
|
+
// After fulu, the decision slot is in previous epoch due to deterministic proposer lookahead
|
|
25
|
+
const epoch = isForkPostFulu(fork) ? state.epochCtx.epoch - 1 : state.epochCtx.epoch;
|
|
26
|
+
const startSlot = computeStartSlotAtEpoch(epoch);
|
|
27
|
+
return Math.max(startSlot - 1, 0);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Returns the block root which decided the attester shuffling for the given `requestedEpoch`.
|
|
31
|
+
* This root can be used to key that attester shuffling.
|
|
32
|
+
*
|
|
33
|
+
* Returns `null` on the one-off scenario where the genesis block decides its own shuffling.
|
|
34
|
+
* It should be set to the latest block applied to this `state` or the genesis block root.
|
|
35
|
+
*/
|
|
36
|
+
export function attesterShufflingDecisionRoot(state, requestedEpoch) {
|
|
37
|
+
const decisionSlot = attesterShufflingDecisionSlot(state, requestedEpoch);
|
|
38
|
+
if (state.slot === decisionSlot) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
return getBlockRootAtSlot(state, decisionSlot);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Returns the slot at which the proposer shuffling was decided. The block root at this slot
|
|
45
|
+
* can be used to key the proposer shuffling for the current epoch.
|
|
46
|
+
*/
|
|
47
|
+
function attesterShufflingDecisionSlot(state, requestedEpoch) {
|
|
48
|
+
const epoch = attesterShufflingDecisionEpoch(state, requestedEpoch);
|
|
49
|
+
const slot = computeStartSlotAtEpoch(epoch);
|
|
50
|
+
return Math.max(slot - 1, 0);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Returns the epoch at which the attester shuffling was decided.
|
|
54
|
+
*
|
|
55
|
+
* Spec ref: https://github.com/ethereum/beacon-APIs/blob/v2.1.0/apis/validator/duties/attester.yaml#L15
|
|
56
|
+
*
|
|
57
|
+
* Throws an error when:
|
|
58
|
+
* - `EpochTooLow` when `requestedEpoch` is more than 1 prior to `currentEpoch`.
|
|
59
|
+
* - `EpochTooHigh` when `requestedEpoch` is more than 1 after `currentEpoch`.
|
|
60
|
+
*/
|
|
61
|
+
function attesterShufflingDecisionEpoch(state, requestedEpoch) {
|
|
62
|
+
const currentEpoch = state.epochCtx.epoch;
|
|
63
|
+
// Next
|
|
64
|
+
if (requestedEpoch === currentEpoch + 1)
|
|
65
|
+
return currentEpoch;
|
|
66
|
+
// Current
|
|
67
|
+
if (requestedEpoch === currentEpoch)
|
|
68
|
+
return Math.max(currentEpoch - 1, 0);
|
|
69
|
+
// Previous
|
|
70
|
+
if (requestedEpoch === currentEpoch - 1)
|
|
71
|
+
return Math.max(currentEpoch - 2, 0);
|
|
72
|
+
if (requestedEpoch < currentEpoch) {
|
|
73
|
+
throw Error(`EpochTooLow: current ${currentEpoch} requested ${requestedEpoch}`);
|
|
74
|
+
}
|
|
75
|
+
throw Error(`EpochTooHigh: current ${currentEpoch} requested ${requestedEpoch}`);
|
|
76
|
+
}
|
|
77
|
+
export function calculateCommitteeAssignments(epochShuffling, requestedValidatorIndices) {
|
|
78
|
+
const requestedValidatorIndicesSet = new Set(requestedValidatorIndices);
|
|
79
|
+
const duties = new Map();
|
|
80
|
+
const epochCommittees = epochShuffling.committees;
|
|
81
|
+
for (let epochSlot = 0; epochSlot < SLOTS_PER_EPOCH; epochSlot++) {
|
|
82
|
+
const slotCommittees = epochCommittees[epochSlot];
|
|
83
|
+
for (let i = 0, committeesAtSlot = slotCommittees.length; i < committeesAtSlot; i++) {
|
|
84
|
+
for (let j = 0, committeeLength = slotCommittees[i].length; j < committeeLength; j++) {
|
|
85
|
+
const validatorIndex = slotCommittees[i][j];
|
|
86
|
+
if (requestedValidatorIndicesSet.has(validatorIndex)) {
|
|
87
|
+
duties.set(validatorIndex, {
|
|
88
|
+
validatorIndex,
|
|
89
|
+
committeeLength,
|
|
90
|
+
committeesAtSlot,
|
|
91
|
+
validatorCommitteeIndex: j,
|
|
92
|
+
committeeIndex: i,
|
|
93
|
+
slot: epochShuffling.epoch * SLOTS_PER_EPOCH + epochSlot,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return duties;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Return the indexed attestation corresponding to ``attestation``.
|
|
103
|
+
*/
|
|
104
|
+
export function getIndexedAttestation(epochShuffling, fork, attestation) {
|
|
105
|
+
const { data } = attestation;
|
|
106
|
+
const attestingIndices = getAttestingIndices(epochShuffling, fork, attestation);
|
|
107
|
+
// sort in-place
|
|
108
|
+
attestingIndices.sort((a, b) => a - b);
|
|
109
|
+
return {
|
|
110
|
+
attestingIndices: attestingIndices,
|
|
111
|
+
data: data,
|
|
112
|
+
signature: attestation.signature,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Return indices of validators who attestested in `attestation`
|
|
117
|
+
*/
|
|
118
|
+
export function getAttestingIndices(epochShuffling, fork, attestation) {
|
|
119
|
+
if (fork < ForkSeq.electra) {
|
|
120
|
+
const { aggregationBits, data } = attestation;
|
|
121
|
+
const validatorIndices = getBeaconCommittee(epochShuffling, data.slot, data.index);
|
|
122
|
+
return aggregationBits.intersectValues(validatorIndices);
|
|
123
|
+
}
|
|
124
|
+
const { aggregationBits, committeeBits, data } = attestation;
|
|
125
|
+
// There is a naming conflict on the term `committeeIndices`
|
|
126
|
+
// In Lodestar it usually means a list of validator indices of participants in a committee
|
|
127
|
+
// In the spec it means a list of committee indices according to committeeBits
|
|
128
|
+
// This `committeeIndices` refers to the latter
|
|
129
|
+
// TODO Electra: resolve the naming conflicts
|
|
130
|
+
const committeeIndices = committeeBits.getTrueBitIndexes();
|
|
131
|
+
const validatorsByCommittee = getBeaconCommittees(epochShuffling, data.slot, committeeIndices);
|
|
132
|
+
// Create a new Uint32Array to flatten `validatorsByCommittee`
|
|
133
|
+
const totalLength = validatorsByCommittee.reduce((acc, curr) => acc + curr.length, 0);
|
|
134
|
+
const committeeValidators = new Uint32Array(totalLength);
|
|
135
|
+
let offset = 0;
|
|
136
|
+
for (const committee of validatorsByCommittee) {
|
|
137
|
+
committeeValidators.set(committee, offset);
|
|
138
|
+
offset += committee.length;
|
|
139
|
+
}
|
|
140
|
+
return aggregationBits.intersectValues(committeeValidators);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Return the beacon committee at slot for index.
|
|
144
|
+
*/
|
|
145
|
+
export function getBeaconCommittee(epochShuffling, slot, index) {
|
|
146
|
+
return getBeaconCommittees(epochShuffling, slot, [index])[0];
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Return a Uint32Array[] representing committees validator indices
|
|
150
|
+
*/
|
|
151
|
+
export function getBeaconCommittees(epochShuffling, slot, indices) {
|
|
152
|
+
if (indices.length === 0) {
|
|
153
|
+
throw new Error("Attempt to get committees without providing CommitteeIndex");
|
|
154
|
+
}
|
|
155
|
+
const slotCommittees = epochShuffling.committees[slot % SLOTS_PER_EPOCH];
|
|
156
|
+
const committees = [];
|
|
157
|
+
for (const index of indices) {
|
|
158
|
+
if (index >= slotCommittees.length) {
|
|
159
|
+
throw new ShufflingError({
|
|
160
|
+
code: ShufflingErrorCode.COMMITTEE_INDEX_OUT_OF_RANGE,
|
|
161
|
+
index,
|
|
162
|
+
maxIndex: slotCommittees.length,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
committees.push(slotCommittees[index]);
|
|
166
|
+
}
|
|
167
|
+
return committees;
|
|
168
|
+
}
|
|
169
|
+
export var ShufflingErrorCode;
|
|
170
|
+
(function (ShufflingErrorCode) {
|
|
171
|
+
ShufflingErrorCode["COMMITTEE_INDEX_OUT_OF_RANGE"] = "SHUFFLING_ERROR_COMMITTEE_INDEX_OUT_OF_RANGE";
|
|
172
|
+
})(ShufflingErrorCode || (ShufflingErrorCode = {}));
|
|
173
|
+
export class ShufflingError extends LodestarError {
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=shuffling.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shuffling.js","sourceRoot":"","sources":["../../src/util/shuffling.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,OAAO,EAAE,eAAe,EAAE,cAAc,EAAC,MAAM,kBAAkB,CAAC;AAWpF,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAE9C,OAAO,EAAC,kBAAkB,EAAC,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAC,uBAAuB,EAAC,MAAM,YAAY,CAAC;AAGnD;;;;;;GAMG;AACH,MAAM,UAAU,6BAA6B,CAAC,IAAc,EAAE,KAAgC;IAC5F,MAAM,YAAY,GAAG,6BAA6B,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAChE,IAAI,KAAK,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;QAChC,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,kBAAkB,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;AACjD,CAAC;AAED;;;GAGG;AACH,SAAS,6BAA6B,CAAC,IAAc,EAAE,KAAgC;IACrF,6FAA6F;IAC7F,MAAM,KAAK,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC;IACrF,MAAM,SAAS,GAAG,uBAAuB,CAAC,KAAK,CAAC,CAAC;IACjD,OAAO,IAAI,CAAC,GAAG,CAAC,SAAS,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;AACpC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAA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aAAiC;CAAG"}
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"bugs": {
|
|
12
12
|
"url": "https://github.com/ChainSafe/lodestar/issues"
|
|
13
13
|
},
|
|
14
|
-
"version": "1.39.0-dev.
|
|
14
|
+
"version": "1.39.0-dev.90334173dc",
|
|
15
15
|
"type": "module",
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
@@ -62,14 +62,14 @@
|
|
|
62
62
|
"@chainsafe/pubkey-index-map": "^3.0.0",
|
|
63
63
|
"@chainsafe/ssz": "^1.2.2",
|
|
64
64
|
"@chainsafe/swap-or-not-shuffle": "^1.2.1",
|
|
65
|
-
"@lodestar/config": "^1.39.0-dev.
|
|
66
|
-
"@lodestar/params": "^1.39.0-dev.
|
|
67
|
-
"@lodestar/types": "^1.39.0-dev.
|
|
68
|
-
"@lodestar/utils": "^1.39.0-dev.
|
|
65
|
+
"@lodestar/config": "^1.39.0-dev.90334173dc",
|
|
66
|
+
"@lodestar/params": "^1.39.0-dev.90334173dc",
|
|
67
|
+
"@lodestar/types": "^1.39.0-dev.90334173dc",
|
|
68
|
+
"@lodestar/utils": "^1.39.0-dev.90334173dc",
|
|
69
69
|
"bigint-buffer": "^1.1.5"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
|
-
"@lodestar/api": "^1.39.0-dev.
|
|
72
|
+
"@lodestar/api": "^1.39.0-dev.90334173dc"
|
|
73
73
|
},
|
|
74
74
|
"keywords": [
|
|
75
75
|
"ethereum",
|
|
@@ -77,5 +77,5 @@
|
|
|
77
77
|
"beacon",
|
|
78
78
|
"blockchain"
|
|
79
79
|
],
|
|
80
|
-
"gitHead": "
|
|
80
|
+
"gitHead": "70ad2460adbb8c8ebd26f4575a7b5ee86c70759e"
|
|
81
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;
|