@lodestar/state-transition 1.39.0-dev.ad129ced66 → 1.39.0-dev.ad81ef1e30

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.
@@ -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;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,CAAC;IAED,MAAM,cAAc,GAAG,cAAc,CAAC,UAAU,CAAC,IAAI,GAAG,eAAe,CAAC,CAAC;IACzE,MAAM,UAAU,GAAG,EAAE,CAAC;IAEtB,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;QAC5B,IAAI,KAAK,IAAI,cAAc,CAAC,MAAM,EAAE,CAAC;YACnC,MAAM,IAAI,cAAc,CAAC;gBACvB,IAAI,EAAE,kBAAkB,CAAC,4BAA4B;gBACrD,KAAK;gBACL,QAAQ,EAAE,cAAc,CAAC,MAAM;aAChC,CAAC,CAAC;QACL,CAAC;QACD,UAAU,CAAC,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC;IACzC,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,MAAM,CAAN,IAAY,kBAEX;AAFD,WAAY,kBAAkB;IAC5B,mGAA6E,CAAA;AAC/E,CAAC,EAFW,kBAAkB,KAAlB,kBAAkB,QAE7B;AAQD,MAAM,OAAO,cAAe,SAAQ,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.ad129ced66",
14
+ "version": "1.39.0-dev.ad81ef1e30",
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.ad129ced66",
66
- "@lodestar/params": "^1.39.0-dev.ad129ced66",
67
- "@lodestar/types": "^1.39.0-dev.ad129ced66",
68
- "@lodestar/utils": "^1.39.0-dev.ad129ced66",
65
+ "@lodestar/config": "^1.39.0-dev.ad81ef1e30",
66
+ "@lodestar/params": "^1.39.0-dev.ad81ef1e30",
67
+ "@lodestar/types": "^1.39.0-dev.ad81ef1e30",
68
+ "@lodestar/utils": "^1.39.0-dev.ad81ef1e30",
69
69
  "bigint-buffer": "^1.1.5"
70
70
  },
71
71
  "devDependencies": {
72
- "@lodestar/api": "^1.39.0-dev.ad129ced66"
72
+ "@lodestar/api": "^1.39.0-dev.ad81ef1e30"
73
73
  },
74
74
  "keywords": [
75
75
  "ethereum",
@@ -77,5 +77,5 @@
77
77
  "beacon",
78
78
  "blockchain"
79
79
  ],
80
- "gitHead": "530782824aadb2d9875d1fa8cbae5428a131e117"
80
+ "gitHead": "293d667378d9bb876ace63236ac81ab0a596257e"
81
81
  }
@@ -23,13 +23,10 @@ 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,
@@ -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";
@@ -752,22 +755,7 @@ export class EpochCache {
752
755
  if (indices.length === 0) {
753
756
  throw new Error("Attempt to get committees without providing CommitteeIndex");
754
757
  }
755
-
756
- const slotCommittees = this.getShufflingAtSlot(slot).committees[slot % SLOTS_PER_EPOCH];
757
- const committees = [];
758
-
759
- for (const index of indices) {
760
- if (index >= slotCommittees.length) {
761
- throw new EpochCacheError({
762
- code: EpochCacheErrorCode.COMMITTEE_INDEX_OUT_OF_RANGE,
763
- index,
764
- maxIndex: slotCommittees.length,
765
- });
766
- }
767
- committees.push(slotCommittees[index]);
768
- }
769
-
770
- return committees;
758
+ return getBeaconCommittees(this.getShufflingAtSlot(slot), slot, indices);
771
759
  }
772
760
 
773
761
  getCommitteeCountPerSlot(epoch: Epoch): number {
@@ -865,50 +853,16 @@ export class EpochCache {
865
853
  * Return the indexed attestation corresponding to ``attestation``.
866
854
  */
867
855
  getIndexedAttestation(fork: ForkSeq, attestation: Attestation): IndexedAttestation {
868
- const {data} = attestation;
869
- const attestingIndices = this.getAttestingIndices(fork, attestation);
870
-
871
- // sort in-place
872
- attestingIndices.sort((a, b) => a - b);
873
- return {
874
- attestingIndices: attestingIndices,
875
- data: data,
876
- signature: attestation.signature,
877
- };
856
+ const shuffling = this.getShufflingAtSlot(attestation.data.slot);
857
+ return getIndexedAttestation(shuffling, fork, attestation);
878
858
  }
879
859
 
880
860
  /**
881
861
  * Return indices of validators who attestested in `attestation`
882
862
  */
883
863
  getAttestingIndices(fork: ForkSeq, attestation: Attestation): number[] {
884
- if (fork < ForkSeq.electra) {
885
- const {aggregationBits, data} = attestation;
886
- const validatorIndices = this.getBeaconCommittee(data.slot, data.index);
887
-
888
- return aggregationBits.intersectValues(validatorIndices);
889
- }
890
- const {aggregationBits, committeeBits, data} = attestation as electra.Attestation;
891
-
892
- // There is a naming conflict on the term `committeeIndices`
893
- // In Lodestar it usually means a list of validator indices of participants in a committee
894
- // In the spec it means a list of committee indices according to committeeBits
895
- // This `committeeIndices` refers to the latter
896
- // TODO Electra: resolve the naming conflicts
897
- const committeeIndices = committeeBits.getTrueBitIndexes();
898
-
899
- const validatorsByCommittee = this.getBeaconCommittees(data.slot, committeeIndices);
900
-
901
- // Create a new Uint32Array to flatten `validatorsByCommittee`
902
- const totalLength = validatorsByCommittee.reduce((acc, curr) => acc + curr.length, 0);
903
- const committeeValidators = new Uint32Array(totalLength);
904
-
905
- let offset = 0;
906
- for (const committee of validatorsByCommittee) {
907
- committeeValidators.set(committee, offset);
908
- offset += committee.length;
909
- }
910
-
911
- return aggregationBits.intersectValues(committeeValidators);
864
+ const shuffling = this.getShufflingAtSlot(attestation.data.slot);
865
+ return getAttestingIndices(shuffling, fork, attestation);
912
866
  }
913
867
 
914
868
  getCommitteeAssignments(
@@ -919,38 +873,6 @@ export class EpochCache {
919
873
  return calculateCommitteeAssignments(shuffling, requestedValidatorIndices);
920
874
  }
921
875
 
922
- /**
923
- * Return the committee assignment in the ``epoch`` for ``validator_index``.
924
- * ``assignment`` returned is a tuple of the following form:
925
- * ``assignment[0]`` is the list of validators in the committee
926
- * ``assignment[1]`` is the index to which the committee is assigned
927
- * ``assignment[2]`` is the slot at which the committee is assigned
928
- * Return null if no assignment..
929
- */
930
- getCommitteeAssignment(epoch: Epoch, validatorIndex: ValidatorIndex): phase0.CommitteeAssignment | null {
931
- if (epoch > this.currentShuffling.epoch + 1) {
932
- throw Error(
933
- `Requesting committee assignment for more than 1 epoch ahead: ${epoch} > ${this.currentShuffling.epoch} + 1`
934
- );
935
- }
936
-
937
- const epochStartSlot = computeStartSlotAtEpoch(epoch);
938
- const committeeCountPerSlot = this.getCommitteeCountPerSlot(epoch);
939
- for (let slot = epochStartSlot; slot < epochStartSlot + SLOTS_PER_EPOCH; slot++) {
940
- for (let i = 0; i < committeeCountPerSlot; i++) {
941
- const committee = this.getBeaconCommittee(slot, i);
942
- if (committee.includes(validatorIndex)) {
943
- return {
944
- validators: Array.from(committee),
945
- committeeIndex: i,
946
- slot,
947
- };
948
- }
949
- }
950
- }
951
- return null;
952
- }
953
-
954
876
  isAggregator(slot: Slot, index: CommitteeIndex, slotSignature: BLSSignature): boolean {
955
877
  const committee = this.getBeaconCommittee(slot, index);
956
878
  return isAggregatorFromCommitteeLength(committee.length, slotSignature);
@@ -1135,7 +1057,6 @@ function getEffectiveBalanceIncrementsByteLen(validatorCount: number): number {
1135
1057
  }
1136
1058
 
1137
1059
  export enum EpochCacheErrorCode {
1138
- COMMITTEE_INDEX_OUT_OF_RANGE = "EPOCH_CONTEXT_ERROR_COMMITTEE_INDEX_OUT_OF_RANGE",
1139
1060
  COMMITTEE_EPOCH_OUT_OF_RANGE = "EPOCH_CONTEXT_ERROR_COMMITTEE_EPOCH_OUT_OF_RANGE",
1140
1061
  DECISION_ROOT_EPOCH_OUT_OF_RANGE = "EPOCH_CONTEXT_ERROR_DECISION_ROOT_EPOCH_OUT_OF_RANGE",
1141
1062
  NEXT_SHUFFLING_NOT_AVAILABLE = "EPOCH_CONTEXT_ERROR_NEXT_SHUFFLING_NOT_AVAILABLE",
@@ -1144,11 +1065,6 @@ export enum EpochCacheErrorCode {
1144
1065
  }
1145
1066
 
1146
1067
  type EpochCacheErrorType =
1147
- | {
1148
- code: EpochCacheErrorCode.COMMITTEE_INDEX_OUT_OF_RANGE;
1149
- index: number;
1150
- maxIndex: number;
1151
- }
1152
1068
  | {
1153
1069
  code: EpochCacheErrorCode.COMMITTEE_EPOCH_OUT_OF_RANGE;
1154
1070
  requestedEpoch: Epoch;
package/src/index.ts CHANGED
@@ -21,8 +21,6 @@ export {
21
21
  } from "./cache/effectiveBalanceIncrements.js";
22
22
  export {
23
23
  EpochCache,
24
- EpochCacheError,
25
- EpochCacheErrorCode,
26
24
  type EpochCacheImmutableData,
27
25
  createEmptyEpochCacheImmutableData,
28
26
  } from "./cache/epochCache.js";
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 "./shufflingDecisionRoot.js";
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"}