@lodestar/state-transition 1.39.0-dev.3bf4734ba9 → 1.39.0-dev.4263377242

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.
Files changed (174) hide show
  1. package/lib/block/index.d.ts.map +1 -1
  2. package/lib/block/index.js +4 -2
  3. package/lib/block/index.js.map +1 -1
  4. package/lib/block/isValidIndexedAttestation.d.ts +5 -5
  5. package/lib/block/isValidIndexedAttestation.d.ts.map +1 -1
  6. package/lib/block/isValidIndexedAttestation.js +9 -10
  7. package/lib/block/isValidIndexedAttestation.js.map +1 -1
  8. package/lib/block/processAttestationPhase0.d.ts.map +1 -1
  9. package/lib/block/processAttestationPhase0.js +1 -1
  10. package/lib/block/processAttestationPhase0.js.map +1 -1
  11. package/lib/block/processAttestationsAltair.d.ts.map +1 -1
  12. package/lib/block/processAttestationsAltair.js +1 -1
  13. package/lib/block/processAttestationsAltair.js.map +1 -1
  14. package/lib/block/processAttesterSlashing.d.ts +3 -2
  15. package/lib/block/processAttesterSlashing.d.ts.map +1 -1
  16. package/lib/block/processAttesterSlashing.js +3 -3
  17. package/lib/block/processAttesterSlashing.js.map +1 -1
  18. package/lib/block/processBlsToExecutionChange.d.ts +3 -1
  19. package/lib/block/processBlsToExecutionChange.d.ts.map +1 -1
  20. package/lib/block/processBlsToExecutionChange.js +7 -11
  21. package/lib/block/processBlsToExecutionChange.js.map +1 -1
  22. package/lib/block/processExecutionPayload.d.ts.map +1 -1
  23. package/lib/block/processExecutionPayload.js +6 -4
  24. package/lib/block/processExecutionPayload.js.map +1 -1
  25. package/lib/block/processProposerSlashing.d.ts +5 -2
  26. package/lib/block/processProposerSlashing.d.ts.map +1 -1
  27. package/lib/block/processProposerSlashing.js +7 -5
  28. package/lib/block/processProposerSlashing.js.map +1 -1
  29. package/lib/block/processRandao.js +2 -2
  30. package/lib/block/processRandao.js.map +1 -1
  31. package/lib/block/processSyncCommittee.d.ts +3 -1
  32. package/lib/block/processSyncCommittee.d.ts.map +1 -1
  33. package/lib/block/processSyncCommittee.js +6 -4
  34. package/lib/block/processSyncCommittee.js.map +1 -1
  35. package/lib/block/processVoluntaryExit.d.ts.map +1 -1
  36. package/lib/block/processVoluntaryExit.js +2 -1
  37. package/lib/block/processVoluntaryExit.js.map +1 -1
  38. package/lib/block/processWithdrawals.d.ts +3 -3
  39. package/lib/block/processWithdrawals.d.ts.map +1 -1
  40. package/lib/block/processWithdrawals.js +152 -105
  41. package/lib/block/processWithdrawals.js.map +1 -1
  42. package/lib/cache/epochCache.d.ts +8 -31
  43. package/lib/cache/epochCache.d.ts.map +1 -1
  44. package/lib/cache/epochCache.js +40 -183
  45. package/lib/cache/epochCache.js.map +1 -1
  46. package/lib/cache/epochTransitionCache.d.ts +5 -12
  47. package/lib/cache/epochTransitionCache.d.ts.map +1 -1
  48. package/lib/cache/epochTransitionCache.js +4 -14
  49. package/lib/cache/epochTransitionCache.js.map +1 -1
  50. package/lib/cache/stateCache.d.ts +1 -0
  51. package/lib/cache/stateCache.d.ts.map +1 -1
  52. package/lib/cache/stateCache.js +1 -2
  53. package/lib/cache/stateCache.js.map +1 -1
  54. package/lib/epoch/processProposerLookahead.d.ts.map +1 -1
  55. package/lib/epoch/processProposerLookahead.js +3 -6
  56. package/lib/epoch/processProposerLookahead.js.map +1 -1
  57. package/lib/index.d.ts +3 -2
  58. package/lib/index.d.ts.map +1 -1
  59. package/lib/index.js +3 -1
  60. package/lib/index.js.map +1 -1
  61. package/lib/rewards/attestationsRewards.d.ts +6 -0
  62. package/lib/rewards/attestationsRewards.d.ts.map +1 -0
  63. package/lib/rewards/attestationsRewards.js +113 -0
  64. package/lib/rewards/attestationsRewards.js.map +1 -0
  65. package/lib/rewards/blockRewards.d.ts +14 -0
  66. package/lib/rewards/blockRewards.d.ts.map +1 -0
  67. package/lib/rewards/blockRewards.js +96 -0
  68. package/lib/rewards/blockRewards.js.map +1 -0
  69. package/lib/rewards/index.d.ts +4 -0
  70. package/lib/rewards/index.d.ts.map +1 -0
  71. package/lib/rewards/index.js +4 -0
  72. package/lib/rewards/index.js.map +1 -0
  73. package/lib/rewards/syncCommitteeRewards.d.ts +6 -0
  74. package/lib/rewards/syncCommitteeRewards.d.ts.map +1 -0
  75. package/lib/rewards/syncCommitteeRewards.js +35 -0
  76. package/lib/rewards/syncCommitteeRewards.js.map +1 -0
  77. package/lib/signatureSets/attesterSlashings.d.ts +5 -5
  78. package/lib/signatureSets/attesterSlashings.d.ts.map +1 -1
  79. package/lib/signatureSets/attesterSlashings.js +10 -7
  80. package/lib/signatureSets/attesterSlashings.js.map +1 -1
  81. package/lib/signatureSets/blsToExecutionChange.d.ts +1 -2
  82. package/lib/signatureSets/blsToExecutionChange.d.ts.map +1 -1
  83. package/lib/signatureSets/blsToExecutionChange.js +2 -2
  84. package/lib/signatureSets/blsToExecutionChange.js.map +1 -1
  85. package/lib/signatureSets/index.d.ts +3 -2
  86. package/lib/signatureSets/index.d.ts.map +1 -1
  87. package/lib/signatureSets/index.js +10 -10
  88. package/lib/signatureSets/index.js.map +1 -1
  89. package/lib/signatureSets/indexedAttestation.d.ts +6 -6
  90. package/lib/signatureSets/indexedAttestation.d.ts.map +1 -1
  91. package/lib/signatureSets/indexedAttestation.js +12 -9
  92. package/lib/signatureSets/indexedAttestation.js.map +1 -1
  93. package/lib/signatureSets/proposer.d.ts +6 -6
  94. package/lib/signatureSets/proposer.d.ts.map +1 -1
  95. package/lib/signatureSets/proposer.js +12 -11
  96. package/lib/signatureSets/proposer.js.map +1 -1
  97. package/lib/signatureSets/proposerSlashings.d.ts +4 -4
  98. package/lib/signatureSets/proposerSlashings.d.ts.map +1 -1
  99. package/lib/signatureSets/proposerSlashings.js +7 -4
  100. package/lib/signatureSets/proposerSlashings.js.map +1 -1
  101. package/lib/signatureSets/randao.d.ts +3 -3
  102. package/lib/signatureSets/randao.d.ts.map +1 -1
  103. package/lib/signatureSets/randao.js +6 -4
  104. package/lib/signatureSets/randao.js.map +1 -1
  105. package/lib/signatureSets/voluntaryExits.d.ts +5 -5
  106. package/lib/signatureSets/voluntaryExits.d.ts.map +1 -1
  107. package/lib/signatureSets/voluntaryExits.js +10 -7
  108. package/lib/signatureSets/voluntaryExits.js.map +1 -1
  109. package/lib/stateTransition.js +1 -1
  110. package/lib/stateTransition.js.map +1 -1
  111. package/lib/types.d.ts +1 -1
  112. package/lib/types.d.ts.map +1 -1
  113. package/lib/util/epochShuffling.d.ts +1 -34
  114. package/lib/util/epochShuffling.d.ts.map +1 -1
  115. package/lib/util/epochShuffling.js +1 -1
  116. package/lib/util/epochShuffling.js.map +1 -1
  117. package/lib/util/execution.d.ts +11 -1
  118. package/lib/util/execution.d.ts.map +1 -1
  119. package/lib/util/execution.js +26 -1
  120. package/lib/util/execution.js.map +1 -1
  121. package/lib/util/index.d.ts +1 -2
  122. package/lib/util/index.d.ts.map +1 -1
  123. package/lib/util/index.js +1 -2
  124. package/lib/util/index.js.map +1 -1
  125. package/lib/util/shuffling.d.ts +58 -0
  126. package/lib/util/shuffling.d.ts.map +1 -0
  127. package/lib/util/shuffling.js +175 -0
  128. package/lib/util/shuffling.js.map +1 -0
  129. package/package.json +14 -11
  130. package/src/block/index.ts +6 -2
  131. package/src/block/isValidIndexedAttestation.ts +22 -12
  132. package/src/block/processAttestationPhase0.ts +3 -1
  133. package/src/block/processAttestationsAltair.ts +7 -1
  134. package/src/block/processAttesterSlashing.ts +16 -4
  135. package/src/block/processBlsToExecutionChange.ts +13 -14
  136. package/src/block/processExecutionPayload.ts +14 -8
  137. package/src/block/processProposerSlashing.ts +21 -6
  138. package/src/block/processRandao.ts +2 -2
  139. package/src/block/processSyncCommittee.ts +15 -4
  140. package/src/block/processVoluntaryExit.ts +4 -1
  141. package/src/block/processWithdrawals.ts +230 -135
  142. package/src/cache/epochCache.ts +52 -217
  143. package/src/cache/epochTransitionCache.ts +9 -34
  144. package/src/cache/stateCache.ts +2 -2
  145. package/src/epoch/processProposerLookahead.ts +3 -7
  146. package/src/index.ts +2 -4
  147. package/src/rewards/attestationsRewards.ts +200 -0
  148. package/src/rewards/blockRewards.ts +150 -0
  149. package/src/rewards/index.ts +3 -0
  150. package/src/rewards/syncCommitteeRewards.ts +56 -0
  151. package/src/signatureSets/attesterSlashings.ts +14 -9
  152. package/src/signatureSets/blsToExecutionChange.ts +2 -3
  153. package/src/signatureSets/index.ts +14 -11
  154. package/src/signatureSets/indexedAttestation.ts +18 -11
  155. package/src/signatureSets/proposer.ts +14 -12
  156. package/src/signatureSets/proposerSlashings.ts +10 -10
  157. package/src/signatureSets/randao.ts +7 -5
  158. package/src/signatureSets/voluntaryExits.ts +14 -9
  159. package/src/stateTransition.ts +1 -1
  160. package/src/types.ts +1 -0
  161. package/src/util/epochShuffling.ts +2 -43
  162. package/src/util/execution.ts +39 -0
  163. package/src/util/index.ts +1 -2
  164. package/src/util/shuffling.ts +234 -0
  165. package/lib/util/calculateCommitteeAssignments.d.ts +0 -12
  166. package/lib/util/calculateCommitteeAssignments.d.ts.map +0 -1
  167. package/lib/util/calculateCommitteeAssignments.js +0 -26
  168. package/lib/util/calculateCommitteeAssignments.js.map +0 -1
  169. package/lib/util/shufflingDecisionRoot.d.ts +0 -20
  170. package/lib/util/shufflingDecisionRoot.d.ts.map +0 -1
  171. package/lib/util/shufflingDecisionRoot.js +0 -76
  172. package/lib/util/shufflingDecisionRoot.js.map +0 -1
  173. package/src/util/calculateCommitteeAssignments.ts +0 -43
  174. package/src/util/shufflingDecisionRoot.ts +0 -81
@@ -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
- IShufflingCache,
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 */
@@ -108,9 +111,6 @@ export class EpochCache {
108
111
  /**
109
112
  * Unique globally shared pubkey registry. There should only exist one for the entire application.
110
113
  *
111
- * TODO: this is a hack, we need a safety mechanism in case a bad eth1 majority vote is in,
112
- * or handle non finalized data differently, or use an immutable.js structure for cheap copies
113
- *
114
114
  * $VALIDATOR_COUNT x 192 char String -> Number Map
115
115
  */
116
116
  pubkey2index: PubkeyIndexMap;
@@ -120,12 +120,6 @@ export class EpochCache {
120
120
  * $VALIDATOR_COUNT x BLST deserialized pubkey (Jacobian coordinates)
121
121
  */
122
122
  index2pubkey: Index2PubkeyCache;
123
- /**
124
- * ShufflingCache is passed in from `beacon-node` so should be available at runtime but may not be
125
- * present during testing.
126
- */
127
- shufflingCache?: IShufflingCache;
128
-
129
123
  /**
130
124
  * Indexes of the block proposers for the current epoch.
131
125
  * For pre-fulu, this is computed and cached from the current shuffling.
@@ -164,7 +158,7 @@ export class EpochCache {
164
158
  /** Same as previousShuffling */
165
159
  currentShuffling: EpochShuffling;
166
160
  /** Same as previousShuffling */
167
- nextShuffling: EpochShuffling | null;
161
+ nextShuffling: EpochShuffling;
168
162
  /**
169
163
  * Cache nextActiveIndices so that in afterProcessEpoch the next shuffling can be build synchronously
170
164
  * in case it is not built or the ShufflingCache is not available
@@ -257,7 +251,6 @@ export class EpochCache {
257
251
  config: BeaconConfig;
258
252
  pubkey2index: PubkeyIndexMap;
259
253
  index2pubkey: Index2PubkeyCache;
260
- shufflingCache?: IShufflingCache;
261
254
  proposers: number[];
262
255
  proposersPrevEpoch: number[] | null;
263
256
  proposersNextEpoch: ProposersDeferred;
@@ -266,7 +259,7 @@ export class EpochCache {
266
259
  nextDecisionRoot: RootHex;
267
260
  previousShuffling: EpochShuffling;
268
261
  currentShuffling: EpochShuffling;
269
- nextShuffling: EpochShuffling | null;
262
+ nextShuffling: EpochShuffling;
270
263
  nextActiveIndices: Uint32Array;
271
264
  effectiveBalanceIncrements: EffectiveBalanceIncrements;
272
265
  totalSlashingsByIncrement: number;
@@ -289,7 +282,6 @@ export class EpochCache {
289
282
  this.config = data.config;
290
283
  this.pubkey2index = data.pubkey2index;
291
284
  this.index2pubkey = data.index2pubkey;
292
- this.shufflingCache = data.shufflingCache;
293
285
  this.proposers = data.proposers;
294
286
  this.proposersPrevEpoch = data.proposersPrevEpoch;
295
287
  this.proposersNextEpoch = data.proposersNextEpoch;
@@ -327,7 +319,7 @@ export class EpochCache {
327
319
  */
328
320
  static createFromState(
329
321
  state: BeaconStateAllForks,
330
- {config, pubkey2index, index2pubkey, shufflingCache}: EpochCacheImmutableData,
322
+ {config, pubkey2index, index2pubkey}: EpochCacheImmutableData,
331
323
  opts?: EpochCacheOpts
332
324
  ): EpochCache {
333
325
  const currentEpoch = computeEpochAtSlot(state.slot);
@@ -354,14 +346,15 @@ export class EpochCache {
354
346
  const currentActiveIndicesAsNumberArray: ValidatorIndex[] = [];
355
347
  const nextActiveIndicesAsNumberArray: ValidatorIndex[] = [];
356
348
 
357
- // BeaconChain could provide a shuffling cache to avoid re-computing shuffling every epoch
349
+ // BeaconChain could provide a shuffling getter to avoid re-computing shuffling every epoch
358
350
  // in that case, we don't need to compute shufflings again
351
+ const shufflingGetter = opts?.shufflingGetter;
359
352
  const previousDecisionRoot = calculateShufflingDecisionRoot(config, state, previousEpoch);
360
- const cachedPreviousShuffling = shufflingCache?.getSync(previousEpoch, previousDecisionRoot);
353
+ const cachedPreviousShuffling = shufflingGetter?.(previousEpoch, previousDecisionRoot);
361
354
  const currentDecisionRoot = calculateShufflingDecisionRoot(config, state, currentEpoch);
362
- const cachedCurrentShuffling = shufflingCache?.getSync(currentEpoch, currentDecisionRoot);
355
+ const cachedCurrentShuffling = shufflingGetter?.(currentEpoch, currentDecisionRoot);
363
356
  const nextDecisionRoot = calculateShufflingDecisionRoot(config, state, nextEpoch);
364
- const cachedNextShuffling = shufflingCache?.getSync(nextEpoch, nextDecisionRoot);
357
+ const cachedNextShuffling = shufflingGetter?.(nextEpoch, nextDecisionRoot);
365
358
 
366
359
  for (let i = 0; i < validatorCount; i++) {
367
360
  const validator = validators[i];
@@ -369,8 +362,7 @@ export class EpochCache {
369
362
  // Note: Not usable for fork-choice balances since in-active validators are not zero'ed
370
363
  effectiveBalanceIncrements[i] = Math.floor(validator.effectiveBalance / EFFECTIVE_BALANCE_INCREMENT);
371
364
 
372
- // we only need to track active indices for previous, current and next epoch if we have to compute shufflings
373
- // skip doing that if we already have cached shufflings
365
+ // Collect active indices for each epoch to compute shufflings
374
366
  if (cachedPreviousShuffling == null && isActiveValidator(validator, previousEpoch)) {
375
367
  previousActiveIndicesAsNumberArray.push(i);
376
368
  }
@@ -405,47 +397,19 @@ export class EpochCache {
405
397
  }
406
398
 
407
399
  const nextActiveIndices = new Uint32Array(nextActiveIndicesAsNumberArray);
408
- let previousShuffling: EpochShuffling;
409
- let currentShuffling: EpochShuffling;
410
- let nextShuffling: EpochShuffling;
411
-
412
- if (!shufflingCache) {
413
- // Only for testing. shufflingCache should always be available in prod
414
- previousShuffling = computeEpochShuffling(
415
- state,
416
- new Uint32Array(previousActiveIndicesAsNumberArray),
417
- previousEpoch
418
- );
419
400
 
420
- currentShuffling = isGenesis
421
- ? previousShuffling
422
- : computeEpochShuffling(state, new Uint32Array(currentActiveIndicesAsNumberArray), currentEpoch);
401
+ // Use cached shufflings if available, otherwise compute
402
+ const currentShuffling =
403
+ cachedCurrentShuffling ??
404
+ computeEpochShuffling(state, new Uint32Array(currentActiveIndicesAsNumberArray), currentEpoch);
423
405
 
424
- nextShuffling = computeEpochShuffling(state, nextActiveIndices, nextEpoch);
425
- } else {
426
- currentShuffling = cachedCurrentShuffling
427
- ? cachedCurrentShuffling
428
- : shufflingCache.getSync(currentEpoch, currentDecisionRoot, {
429
- state,
430
- activeIndices: new Uint32Array(currentActiveIndicesAsNumberArray),
431
- });
432
-
433
- previousShuffling = cachedPreviousShuffling
434
- ? cachedPreviousShuffling
435
- : isGenesis
436
- ? currentShuffling
437
- : shufflingCache.getSync(previousEpoch, previousDecisionRoot, {
438
- state,
439
- activeIndices: new Uint32Array(previousActiveIndicesAsNumberArray),
440
- });
441
-
442
- nextShuffling = cachedNextShuffling
443
- ? cachedNextShuffling
444
- : shufflingCache.getSync(nextEpoch, nextDecisionRoot, {
445
- state,
446
- activeIndices: nextActiveIndices,
447
- });
448
- }
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);
449
413
 
450
414
  const currentProposerSeed = getSeed(state, currentEpoch, DOMAIN_BEACON_PROPOSER);
451
415
 
@@ -552,7 +516,6 @@ export class EpochCache {
552
516
  config,
553
517
  pubkey2index,
554
518
  index2pubkey,
555
- shufflingCache,
556
519
  proposers,
557
520
  // On first epoch, set to null to prevent unnecessary work since this is only used for metrics
558
521
  proposersPrevEpoch: null,
@@ -596,7 +559,6 @@ export class EpochCache {
596
559
  // Common append-only structures shared with all states, no need to clone
597
560
  pubkey2index: this.pubkey2index,
598
561
  index2pubkey: this.index2pubkey,
599
- shufflingCache: this.shufflingCache,
600
562
  // Immutable data
601
563
  proposers: this.proposers,
602
564
  proposersPrevEpoch: this.proposersPrevEpoch,
@@ -655,62 +617,26 @@ export class EpochCache {
655
617
  this.previousShuffling = this.currentShuffling;
656
618
  this.previousDecisionRoot = this.currentDecisionRoot;
657
619
 
658
- // move next to current or calculate upcoming
620
+ // move next to current
659
621
  this.currentDecisionRoot = this.nextDecisionRoot;
660
- if (this.nextShuffling) {
661
- // was already pulled from the ShufflingCache to the EpochCache (should be in most cases)
662
- this.currentShuffling = this.nextShuffling;
663
- } else {
664
- this.shufflingCache?.metrics?.shufflingCache.nextShufflingNotOnEpochCache.inc();
665
- this.currentShuffling =
666
- this.shufflingCache?.getSync(upcomingEpoch, this.currentDecisionRoot, {
667
- state,
668
- // have to use the "nextActiveIndices" that were saved in the last transition here to calculate
669
- // the upcoming shuffling if it is not already built (similar condition to the below computation)
670
- activeIndices: this.nextActiveIndices,
671
- }) ??
672
- // allow for this case during testing where the ShufflingCache is not present, may affect perf testing
673
- // so should be taken into account when structuring tests. Should not affect unit or other tests though
674
- computeEpochShuffling(state, this.nextActiveIndices, upcomingEpoch);
675
- }
622
+ this.currentShuffling = this.nextShuffling;
676
623
 
677
- // handle next values
678
- this.nextDecisionRoot = epochTransitionCache.nextShufflingDecisionRoot;
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);
679
637
  this.nextActiveIndices = epochTransitionCache.nextShufflingActiveIndices;
680
- if (this.shufflingCache) {
681
- if (!epochTransitionCache.asyncShufflingCalculation) {
682
- this.nextShuffling = this.shufflingCache.getSync(epochAfterUpcoming, this.nextDecisionRoot, {
683
- state,
684
- activeIndices: this.nextActiveIndices,
685
- });
686
- } else {
687
- this.nextShuffling = null;
688
- // This promise will resolve immediately after the synchronous code of the state-transition runs. Until
689
- // the build is done on a worker thread it will be calculated immediately after the epoch transition
690
- // completes. Once the work is done concurrently it should be ready by time this get runs so the promise
691
- // will resolve directly on the next spin of the event loop because the epoch transition and shuffling take
692
- // about the same time to calculate so theoretically its ready now. Do not await here though in case it
693
- // is not ready yet as the transition must not be asynchronous.
694
- this.shufflingCache
695
- .get(epochAfterUpcoming, this.nextDecisionRoot)
696
- .then((shuffling) => {
697
- if (!shuffling) {
698
- throw new Error("EpochShuffling not returned from get in afterProcessEpoch");
699
- }
700
- this.nextShuffling = shuffling;
701
- })
702
- .catch((err) => {
703
- this.shufflingCache?.logger?.error(
704
- "EPOCH_CONTEXT_SHUFFLING_BUILD_ERROR",
705
- {epoch: epochAfterUpcoming, decisionRoot: epochTransitionCache.nextShufflingDecisionRoot},
706
- err
707
- );
708
- });
709
- }
710
- } else {
711
- // Only for testing. shufflingCache should always be available in prod
712
- this.nextShuffling = computeEpochShuffling(state, this.nextActiveIndices, epochAfterUpcoming);
713
- }
638
+ this.nextShuffling =
639
+ epochTransitionCache.nextShuffling ?? computeEpochShuffling(state, this.nextActiveIndices, epochAfterUpcoming);
714
640
 
715
641
  // TODO: DEDUPLICATE from createEpochCache
716
642
  //
@@ -829,22 +755,7 @@ export class EpochCache {
829
755
  if (indices.length === 0) {
830
756
  throw new Error("Attempt to get committees without providing CommitteeIndex");
831
757
  }
832
-
833
- const slotCommittees = this.getShufflingAtSlot(slot).committees[slot % SLOTS_PER_EPOCH];
834
- const committees = [];
835
-
836
- for (const index of indices) {
837
- if (index >= slotCommittees.length) {
838
- throw new EpochCacheError({
839
- code: EpochCacheErrorCode.COMMITTEE_INDEX_OUT_OF_RANGE,
840
- index,
841
- maxIndex: slotCommittees.length,
842
- });
843
- }
844
- committees.push(slotCommittees[index]);
845
- }
846
-
847
- return committees;
758
+ return getBeaconCommittees(this.getShufflingAtSlot(slot), slot, indices);
848
759
  }
849
760
 
850
761
  getCommitteeCountPerSlot(epoch: Epoch): number {
@@ -942,50 +853,16 @@ export class EpochCache {
942
853
  * Return the indexed attestation corresponding to ``attestation``.
943
854
  */
944
855
  getIndexedAttestation(fork: ForkSeq, attestation: Attestation): IndexedAttestation {
945
- const {data} = attestation;
946
- const attestingIndices = this.getAttestingIndices(fork, attestation);
947
-
948
- // sort in-place
949
- attestingIndices.sort((a, b) => a - b);
950
- return {
951
- attestingIndices: attestingIndices,
952
- data: data,
953
- signature: attestation.signature,
954
- };
856
+ const shuffling = this.getShufflingAtSlot(attestation.data.slot);
857
+ return getIndexedAttestation(shuffling, fork, attestation);
955
858
  }
956
859
 
957
860
  /**
958
861
  * Return indices of validators who attestested in `attestation`
959
862
  */
960
863
  getAttestingIndices(fork: ForkSeq, attestation: Attestation): number[] {
961
- if (fork < ForkSeq.electra) {
962
- const {aggregationBits, data} = attestation;
963
- const validatorIndices = this.getBeaconCommittee(data.slot, data.index);
964
-
965
- return aggregationBits.intersectValues(validatorIndices);
966
- }
967
- const {aggregationBits, committeeBits, data} = attestation as electra.Attestation;
968
-
969
- // There is a naming conflict on the term `committeeIndices`
970
- // In Lodestar it usually means a list of validator indices of participants in a committee
971
- // In the spec it means a list of committee indices according to committeeBits
972
- // This `committeeIndices` refers to the latter
973
- // TODO Electra: resolve the naming conflicts
974
- const committeeIndices = committeeBits.getTrueBitIndexes();
975
-
976
- const validatorsByCommittee = this.getBeaconCommittees(data.slot, committeeIndices);
977
-
978
- // Create a new Uint32Array to flatten `validatorsByCommittee`
979
- const totalLength = validatorsByCommittee.reduce((acc, curr) => acc + curr.length, 0);
980
- const committeeValidators = new Uint32Array(totalLength);
981
-
982
- let offset = 0;
983
- for (const committee of validatorsByCommittee) {
984
- committeeValidators.set(committee, offset);
985
- offset += committee.length;
986
- }
987
-
988
- return aggregationBits.intersectValues(committeeValidators);
864
+ const shuffling = this.getShufflingAtSlot(attestation.data.slot);
865
+ return getAttestingIndices(shuffling, fork, attestation);
989
866
  }
990
867
 
991
868
  getCommitteeAssignments(
@@ -996,38 +873,6 @@ export class EpochCache {
996
873
  return calculateCommitteeAssignments(shuffling, requestedValidatorIndices);
997
874
  }
998
875
 
999
- /**
1000
- * Return the committee assignment in the ``epoch`` for ``validator_index``.
1001
- * ``assignment`` returned is a tuple of the following form:
1002
- * ``assignment[0]`` is the list of validators in the committee
1003
- * ``assignment[1]`` is the index to which the committee is assigned
1004
- * ``assignment[2]`` is the slot at which the committee is assigned
1005
- * Return null if no assignment..
1006
- */
1007
- getCommitteeAssignment(epoch: Epoch, validatorIndex: ValidatorIndex): phase0.CommitteeAssignment | null {
1008
- if (epoch > this.currentShuffling.epoch + 1) {
1009
- throw Error(
1010
- `Requesting committee assignment for more than 1 epoch ahead: ${epoch} > ${this.currentShuffling.epoch} + 1`
1011
- );
1012
- }
1013
-
1014
- const epochStartSlot = computeStartSlotAtEpoch(epoch);
1015
- const committeeCountPerSlot = this.getCommitteeCountPerSlot(epoch);
1016
- for (let slot = epochStartSlot; slot < epochStartSlot + SLOTS_PER_EPOCH; slot++) {
1017
- for (let i = 0; i < committeeCountPerSlot; i++) {
1018
- const committee = this.getBeaconCommittee(slot, i);
1019
- if (committee.includes(validatorIndex)) {
1020
- return {
1021
- validators: Array.from(committee),
1022
- committeeIndex: i,
1023
- slot,
1024
- };
1025
- }
1026
- }
1027
- }
1028
- return null;
1029
- }
1030
-
1031
876
  isAggregator(slot: Slot, index: CommitteeIndex, slotSignature: BLSSignature): boolean {
1032
877
  const committee = this.getBeaconCommittee(slot, index);
1033
878
  return isAggregatorFromCommitteeLength(committee.length, slotSignature);
@@ -1103,10 +948,6 @@ export class EpochCache {
1103
948
  case this.epoch:
1104
949
  return this.currentShuffling;
1105
950
  case this.nextEpoch:
1106
- if (!this.nextShuffling) {
1107
- this.nextShuffling =
1108
- this.shufflingCache?.getSync(this.nextEpoch, this.getShufflingDecisionRoot(this.nextEpoch)) ?? null;
1109
- }
1110
951
  return this.nextShuffling;
1111
952
  default:
1112
953
  return null;
@@ -1216,7 +1057,6 @@ function getEffectiveBalanceIncrementsByteLen(validatorCount: number): number {
1216
1057
  }
1217
1058
 
1218
1059
  export enum EpochCacheErrorCode {
1219
- COMMITTEE_INDEX_OUT_OF_RANGE = "EPOCH_CONTEXT_ERROR_COMMITTEE_INDEX_OUT_OF_RANGE",
1220
1060
  COMMITTEE_EPOCH_OUT_OF_RANGE = "EPOCH_CONTEXT_ERROR_COMMITTEE_EPOCH_OUT_OF_RANGE",
1221
1061
  DECISION_ROOT_EPOCH_OUT_OF_RANGE = "EPOCH_CONTEXT_ERROR_DECISION_ROOT_EPOCH_OUT_OF_RANGE",
1222
1062
  NEXT_SHUFFLING_NOT_AVAILABLE = "EPOCH_CONTEXT_ERROR_NEXT_SHUFFLING_NOT_AVAILABLE",
@@ -1225,11 +1065,6 @@ export enum EpochCacheErrorCode {
1225
1065
  }
1226
1066
 
1227
1067
  type EpochCacheErrorType =
1228
- | {
1229
- code: EpochCacheErrorCode.COMMITTEE_INDEX_OUT_OF_RANGE;
1230
- index: number;
1231
- maxIndex: number;
1232
- }
1233
1068
  | {
1234
1069
  code: EpochCacheErrorCode.COMMITTEE_EPOCH_OUT_OF_RANGE;
1235
1070
  requestedEpoch: Epoch;
@@ -1,12 +1,6 @@
1
- import {
2
- EPOCHS_PER_SLASHINGS_VECTOR,
3
- FAR_FUTURE_EPOCH,
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
- * Shuffling decision root that gets set on the EpochCache in afterProcessEpoch
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
- nextShufflingDecisionRoot: RootHex;
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
- // Trigger async build of shuffling for epoch after next (nextShuffling post epoch transition)
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
- asyncShufflingCalculation,
504
+ nextShuffling: null,
530
505
  // to be updated in processEffectiveBalanceUpdates
531
506
  nextEpochTotalActiveBalanceByIncrement: 0,
532
507
  isActivePrevEpoch,
@@ -17,6 +17,7 @@ import {
17
17
  } from "./types.js";
18
18
 
19
19
  export type BeaconStateCache = {
20
+ /** @deprecated should not access config outside of state-transition package */
20
21
  config: BeaconConfig;
21
22
  epochCtx: EpochCache;
22
23
  /** Count of clones created from this BeaconStateCache instance. readonly to prevent accidental usage downstream */
@@ -179,7 +180,7 @@ export function loadCachedBeaconState<T extends BeaconStateAllForks & BeaconStat
179
180
  stateBytes,
180
181
  seedValidatorsBytes
181
182
  );
182
- const {pubkey2index, index2pubkey, shufflingCache} = cachedSeedState.epochCtx;
183
+ const {pubkey2index, index2pubkey} = cachedSeedState.epochCtx;
183
184
  // Get the validators sub tree once for all the loop
184
185
  const validators = migratedState.validators;
185
186
  for (const validatorIndex of modifiedValidators) {
@@ -195,7 +196,6 @@ export function loadCachedBeaconState<T extends BeaconStateAllForks & BeaconStat
195
196
  config: cachedSeedState.config,
196
197
  pubkey2index,
197
198
  index2pubkey,
198
- shufflingCache,
199
199
  },
200
200
  {...(opts ?? {}), ...{skipSyncPubkeys: true}}
201
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
- state.epochCtx.shufflingCache?.getSync(epoch, cache.nextShufflingDecisionRoot, {
27
- state,
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
@@ -21,14 +21,11 @@ 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";
29
27
  export {type EpochTransitionCache, beforeProcessEpoch} from "./cache/epochTransitionCache.js";
30
- // Aux data-structures
31
- export {type Index2PubkeyCache} from "./cache/pubkeyCache.js";
28
+ export {type Index2PubkeyCache, syncPubkeys} from "./cache/pubkeyCache.js";
32
29
  // Main state caches
33
30
  export {
34
31
  type BeaconStateCache,
@@ -41,6 +38,7 @@ export {
41
38
  export * from "./constants/index.js";
42
39
  export type {EpochTransitionStep} from "./epoch/index.js";
43
40
  export {type BeaconStateTransitionMetrics, getMetrics} from "./metrics.js";
41
+ export * from "./rewards/index.js";
44
42
  export * from "./signatureSets/index.js";
45
43
  export * from "./stateTransition.js";
46
44
  export type {