@lodestar/state-transition 1.39.1 → 1.40.0-dev.1020f27ca9

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 (218) hide show
  1. package/lib/block/index.d.ts +1 -0
  2. package/lib/block/index.d.ts.map +1 -1
  3. package/lib/block/index.js +1 -0
  4. package/lib/block/index.js.map +1 -1
  5. package/lib/block/isValidIndexedAttestation.d.ts +4 -5
  6. package/lib/block/isValidIndexedAttestation.d.ts.map +1 -1
  7. package/lib/block/isValidIndexedAttestation.js +9 -10
  8. package/lib/block/isValidIndexedAttestation.js.map +1 -1
  9. package/lib/block/isValidIndexedPayloadAttestation.d.ts.map +1 -1
  10. package/lib/block/isValidIndexedPayloadAttestation.js +1 -1
  11. package/lib/block/isValidIndexedPayloadAttestation.js.map +1 -1
  12. package/lib/block/processAttestationPhase0.d.ts.map +1 -1
  13. package/lib/block/processAttestationPhase0.js +1 -1
  14. package/lib/block/processAttestationPhase0.js.map +1 -1
  15. package/lib/block/processAttestationsAltair.d.ts.map +1 -1
  16. package/lib/block/processAttestationsAltair.js +2 -2
  17. package/lib/block/processAttestationsAltair.js.map +1 -1
  18. package/lib/block/processAttesterSlashing.d.ts +3 -2
  19. package/lib/block/processAttesterSlashing.d.ts.map +1 -1
  20. package/lib/block/processAttesterSlashing.js +3 -3
  21. package/lib/block/processAttesterSlashing.js.map +1 -1
  22. package/lib/block/processBlsToExecutionChange.d.ts +3 -1
  23. package/lib/block/processBlsToExecutionChange.d.ts.map +1 -1
  24. package/lib/block/processBlsToExecutionChange.js +7 -11
  25. package/lib/block/processBlsToExecutionChange.js.map +1 -1
  26. package/lib/block/processConsolidationRequest.d.ts +1 -2
  27. package/lib/block/processConsolidationRequest.d.ts.map +1 -1
  28. package/lib/block/processConsolidationRequest.js +2 -2
  29. package/lib/block/processConsolidationRequest.js.map +1 -1
  30. package/lib/block/processDepositRequest.d.ts +8 -2
  31. package/lib/block/processDepositRequest.d.ts.map +1 -1
  32. package/lib/block/processDepositRequest.js +81 -8
  33. package/lib/block/processDepositRequest.js.map +1 -1
  34. package/lib/block/processExecutionPayloadBid.d.ts.map +1 -1
  35. package/lib/block/processExecutionPayloadBid.js +16 -29
  36. package/lib/block/processExecutionPayloadBid.js.map +1 -1
  37. package/lib/block/processExecutionPayloadEnvelope.d.ts.map +1 -1
  38. package/lib/block/processExecutionPayloadEnvelope.js +26 -25
  39. package/lib/block/processExecutionPayloadEnvelope.js.map +1 -1
  40. package/lib/block/processOperations.js +2 -2
  41. package/lib/block/processOperations.js.map +1 -1
  42. package/lib/block/processProposerSlashing.d.ts +5 -2
  43. package/lib/block/processProposerSlashing.d.ts.map +1 -1
  44. package/lib/block/processProposerSlashing.js +8 -6
  45. package/lib/block/processProposerSlashing.js.map +1 -1
  46. package/lib/block/processSyncCommittee.d.ts +1 -2
  47. package/lib/block/processSyncCommittee.d.ts.map +1 -1
  48. package/lib/block/processSyncCommittee.js +5 -5
  49. package/lib/block/processSyncCommittee.js.map +1 -1
  50. package/lib/block/processVoluntaryExit.d.ts +1 -1
  51. package/lib/block/processVoluntaryExit.d.ts.map +1 -1
  52. package/lib/block/processVoluntaryExit.js +45 -3
  53. package/lib/block/processVoluntaryExit.js.map +1 -1
  54. package/lib/block/processWithdrawalRequest.js +1 -1
  55. package/lib/block/processWithdrawalRequest.js.map +1 -1
  56. package/lib/block/processWithdrawals.d.ts +1 -0
  57. package/lib/block/processWithdrawals.d.ts.map +1 -1
  58. package/lib/block/processWithdrawals.js +121 -66
  59. package/lib/block/processWithdrawals.js.map +1 -1
  60. package/lib/cache/epochCache.d.ts +8 -28
  61. package/lib/cache/epochCache.d.ts.map +1 -1
  62. package/lib/cache/epochCache.js +40 -180
  63. package/lib/cache/epochCache.js.map +1 -1
  64. package/lib/cache/epochTransitionCache.d.ts +5 -12
  65. package/lib/cache/epochTransitionCache.d.ts.map +1 -1
  66. package/lib/cache/epochTransitionCache.js +4 -14
  67. package/lib/cache/epochTransitionCache.js.map +1 -1
  68. package/lib/cache/stateCache.d.ts.map +1 -1
  69. package/lib/cache/stateCache.js +1 -2
  70. package/lib/cache/stateCache.js.map +1 -1
  71. package/lib/epoch/processBuilderPendingPayments.d.ts.map +1 -1
  72. package/lib/epoch/processBuilderPendingPayments.js +1 -4
  73. package/lib/epoch/processBuilderPendingPayments.js.map +1 -1
  74. package/lib/epoch/processProposerLookahead.d.ts.map +1 -1
  75. package/lib/epoch/processProposerLookahead.js +3 -6
  76. package/lib/epoch/processProposerLookahead.js.map +1 -1
  77. package/lib/index.d.ts +1 -1
  78. package/lib/index.d.ts.map +1 -1
  79. package/lib/index.js +1 -1
  80. package/lib/index.js.map +1 -1
  81. package/lib/rewards/blockRewards.d.ts +2 -1
  82. package/lib/rewards/blockRewards.d.ts.map +1 -1
  83. package/lib/rewards/blockRewards.js +3 -2
  84. package/lib/rewards/blockRewards.js.map +1 -1
  85. package/lib/rewards/syncCommitteeRewards.d.ts.map +1 -1
  86. package/lib/rewards/syncCommitteeRewards.js +10 -11
  87. package/lib/rewards/syncCommitteeRewards.js.map +1 -1
  88. package/lib/signatureSets/attesterSlashings.d.ts +3 -4
  89. package/lib/signatureSets/attesterSlashings.d.ts.map +1 -1
  90. package/lib/signatureSets/attesterSlashings.js +6 -6
  91. package/lib/signatureSets/attesterSlashings.js.map +1 -1
  92. package/lib/signatureSets/blsToExecutionChange.d.ts +4 -5
  93. package/lib/signatureSets/blsToExecutionChange.d.ts.map +1 -1
  94. package/lib/signatureSets/blsToExecutionChange.js +2 -2
  95. package/lib/signatureSets/blsToExecutionChange.js.map +1 -1
  96. package/lib/signatureSets/executionPayloadBid.d.ts +4 -0
  97. package/lib/signatureSets/executionPayloadBid.d.ts.map +1 -0
  98. package/lib/signatureSets/executionPayloadBid.js +8 -0
  99. package/lib/signatureSets/executionPayloadBid.js.map +1 -0
  100. package/lib/signatureSets/executionPayloadEnvelope.d.ts +4 -0
  101. package/lib/signatureSets/executionPayloadEnvelope.d.ts.map +1 -0
  102. package/lib/signatureSets/executionPayloadEnvelope.js +8 -0
  103. package/lib/signatureSets/executionPayloadEnvelope.js.map +1 -0
  104. package/lib/signatureSets/index.d.ts +3 -2
  105. package/lib/signatureSets/index.d.ts.map +1 -1
  106. package/lib/signatureSets/index.js +10 -8
  107. package/lib/signatureSets/index.js.map +1 -1
  108. package/lib/signatureSets/indexedAttestation.d.ts +3 -4
  109. package/lib/signatureSets/indexedAttestation.d.ts.map +1 -1
  110. package/lib/signatureSets/indexedAttestation.js +6 -6
  111. package/lib/signatureSets/indexedAttestation.js.map +1 -1
  112. package/lib/signatureSets/indexedPayloadAttestation.d.ts +5 -4
  113. package/lib/signatureSets/indexedPayloadAttestation.d.ts.map +1 -1
  114. package/lib/signatureSets/indexedPayloadAttestation.js +3 -3
  115. package/lib/signatureSets/indexedPayloadAttestation.js.map +1 -1
  116. package/lib/signatureSets/proposer.d.ts +3 -3
  117. package/lib/signatureSets/proposer.d.ts.map +1 -1
  118. package/lib/signatureSets/proposer.js +12 -12
  119. package/lib/signatureSets/proposer.js.map +1 -1
  120. package/lib/signatureSets/proposerSlashings.d.ts +2 -3
  121. package/lib/signatureSets/proposerSlashings.d.ts.map +1 -1
  122. package/lib/signatureSets/proposerSlashings.js +6 -6
  123. package/lib/signatureSets/proposerSlashings.js.map +1 -1
  124. package/lib/signatureSets/randao.d.ts +1 -1
  125. package/lib/signatureSets/randao.d.ts.map +1 -1
  126. package/lib/signatureSets/randao.js +4 -4
  127. package/lib/signatureSets/randao.js.map +1 -1
  128. package/lib/signatureSets/voluntaryExits.d.ts +2 -2
  129. package/lib/signatureSets/voluntaryExits.d.ts.map +1 -1
  130. package/lib/signatureSets/voluntaryExits.js +6 -6
  131. package/lib/signatureSets/voluntaryExits.js.map +1 -1
  132. package/lib/types.d.ts +1 -1
  133. package/lib/types.d.ts.map +1 -1
  134. package/lib/util/electra.d.ts.map +1 -1
  135. package/lib/util/electra.js +1 -2
  136. package/lib/util/electra.js.map +1 -1
  137. package/lib/util/epochShuffling.d.ts +1 -34
  138. package/lib/util/epochShuffling.d.ts.map +1 -1
  139. package/lib/util/epochShuffling.js +1 -1
  140. package/lib/util/epochShuffling.js.map +1 -1
  141. package/lib/util/gloas.d.ts +46 -5
  142. package/lib/util/gloas.d.ts.map +1 -1
  143. package/lib/util/gloas.js +91 -5
  144. package/lib/util/gloas.js.map +1 -1
  145. package/lib/util/index.d.ts +2 -2
  146. package/lib/util/index.d.ts.map +1 -1
  147. package/lib/util/index.js +2 -2
  148. package/lib/util/index.js.map +1 -1
  149. package/lib/util/interop.js +1 -1
  150. package/lib/util/interop.js.map +1 -1
  151. package/lib/util/shuffling.d.ts +58 -0
  152. package/lib/util/shuffling.d.ts.map +1 -0
  153. package/lib/util/shuffling.js +175 -0
  154. package/lib/util/shuffling.js.map +1 -0
  155. package/lib/util/signatureSets.d.ts +38 -5
  156. package/lib/util/signatureSets.d.ts.map +1 -1
  157. package/lib/util/signatureSets.js +48 -6
  158. package/lib/util/signatureSets.js.map +1 -1
  159. package/lib/util/validator.d.ts +6 -1
  160. package/lib/util/validator.d.ts.map +1 -1
  161. package/lib/util/validator.js +26 -16
  162. package/lib/util/validator.js.map +1 -1
  163. package/package.json +8 -8
  164. package/src/block/index.ts +1 -0
  165. package/src/block/isValidIndexedAttestation.ts +18 -12
  166. package/src/block/isValidIndexedPayloadAttestation.ts +4 -1
  167. package/src/block/processAttestationPhase0.ts +2 -1
  168. package/src/block/processAttestationsAltair.ts +2 -8
  169. package/src/block/processAttesterSlashing.ts +16 -4
  170. package/src/block/processBlsToExecutionChange.ts +13 -14
  171. package/src/block/processConsolidationRequest.ts +2 -3
  172. package/src/block/processDepositRequest.ts +101 -8
  173. package/src/block/processExecutionPayloadBid.ts +20 -42
  174. package/src/block/processExecutionPayloadEnvelope.ts +34 -30
  175. package/src/block/processOperations.ts +2 -2
  176. package/src/block/processProposerSlashing.ts +22 -12
  177. package/src/block/processSyncCommittee.ts +3 -6
  178. package/src/block/processVoluntaryExit.ts +60 -5
  179. package/src/block/processWithdrawalRequest.ts +1 -1
  180. package/src/block/processWithdrawals.ts +168 -70
  181. package/src/cache/epochCache.ts +52 -214
  182. package/src/cache/epochTransitionCache.ts +9 -34
  183. package/src/cache/stateCache.ts +1 -2
  184. package/src/epoch/processBuilderPendingPayments.ts +1 -5
  185. package/src/epoch/processProposerLookahead.ts +3 -7
  186. package/src/index.ts +0 -2
  187. package/src/rewards/blockRewards.ts +6 -3
  188. package/src/rewards/syncCommitteeRewards.ts +10 -13
  189. package/src/signatureSets/attesterSlashings.ts +3 -7
  190. package/src/signatureSets/blsToExecutionChange.ts +5 -6
  191. package/src/signatureSets/executionPayloadBid.ts +14 -0
  192. package/src/signatureSets/executionPayloadEnvelope.ts +13 -0
  193. package/src/signatureSets/index.ts +8 -9
  194. package/src/signatureSets/indexedAttestation.ts +2 -7
  195. package/src/signatureSets/indexedPayloadAttestation.ts +9 -7
  196. package/src/signatureSets/proposer.ts +8 -12
  197. package/src/signatureSets/proposerSlashings.ts +4 -7
  198. package/src/signatureSets/randao.ts +4 -8
  199. package/src/signatureSets/voluntaryExits.ts +5 -10
  200. package/src/types.ts +1 -0
  201. package/src/util/electra.ts +1 -4
  202. package/src/util/epochShuffling.ts +2 -43
  203. package/src/util/gloas.ts +116 -10
  204. package/src/util/index.ts +2 -2
  205. package/src/util/interop.ts +1 -1
  206. package/src/util/shuffling.ts +234 -0
  207. package/src/util/signatureSets.ts +84 -8
  208. package/src/util/validator.ts +31 -16
  209. package/lib/util/calculateCommitteeAssignments.d.ts +0 -12
  210. package/lib/util/calculateCommitteeAssignments.d.ts.map +0 -1
  211. package/lib/util/calculateCommitteeAssignments.js +0 -26
  212. package/lib/util/calculateCommitteeAssignments.js.map +0 -1
  213. package/lib/util/shufflingDecisionRoot.d.ts +0 -20
  214. package/lib/util/shufflingDecisionRoot.d.ts.map +0 -1
  215. package/lib/util/shufflingDecisionRoot.js +0 -76
  216. package/lib/util/shufflingDecisionRoot.js.map +0 -1
  217. package/src/util/calculateCommitteeAssignments.ts +0 -43
  218. 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 */
@@ -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 | null;
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 | null;
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, shufflingCache}: EpochCacheImmutableData,
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 cache to avoid re-computing shuffling every epoch
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 = shufflingCache?.getSync(previousEpoch, previousDecisionRoot);
353
+ const cachedPreviousShuffling = shufflingGetter?.(previousEpoch, previousDecisionRoot);
358
354
  const currentDecisionRoot = calculateShufflingDecisionRoot(config, state, currentEpoch);
359
- const cachedCurrentShuffling = shufflingCache?.getSync(currentEpoch, currentDecisionRoot);
355
+ const cachedCurrentShuffling = shufflingGetter?.(currentEpoch, currentDecisionRoot);
360
356
  const nextDecisionRoot = calculateShufflingDecisionRoot(config, state, nextEpoch);
361
- const cachedNextShuffling = shufflingCache?.getSync(nextEpoch, nextDecisionRoot);
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
- // we only need to track active indices for previous, current and next epoch if we have to compute shufflings
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
- currentShuffling = isGenesis
418
- ? previousShuffling
419
- : 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);
420
405
 
421
- nextShuffling = computeEpochShuffling(state, nextActiveIndices, nextEpoch);
422
- } else {
423
- currentShuffling = cachedCurrentShuffling
424
- ? cachedCurrentShuffling
425
- : shufflingCache.getSync(currentEpoch, currentDecisionRoot, {
426
- state,
427
- activeIndices: new Uint32Array(currentActiveIndicesAsNumberArray),
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 or calculate upcoming
620
+ // move next to current
656
621
  this.currentDecisionRoot = this.nextDecisionRoot;
657
- if (this.nextShuffling) {
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
- // handle next values
675
- 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);
676
637
  this.nextActiveIndices = epochTransitionCache.nextShufflingActiveIndices;
677
- if (this.shufflingCache) {
678
- if (!epochTransitionCache.asyncShufflingCalculation) {
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 {data} = attestation;
943
- const attestingIndices = this.getAttestingIndices(fork, attestation);
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
- if (fork < ForkSeq.electra) {
959
- const {aggregationBits, data} = attestation;
960
- const validatorIndices = this.getBeaconCommittee(data.slot, data.index);
961
-
962
- return aggregationBits.intersectValues(validatorIndices);
963
- }
964
- const {aggregationBits, committeeBits, data} = attestation as electra.Attestation;
965
-
966
- // There is a naming conflict on the term `committeeIndices`
967
- // In Lodestar it usually means a list of validator indices of participants in a committee
968
- // In the spec it means a list of committee indices according to committeeBits
969
- // This `committeeIndices` refers to the latter
970
- // TODO Electra: resolve the naming conflicts
971
- const committeeIndices = committeeBits.getTrueBitIndexes();
972
-
973
- const validatorsByCommittee = this.getBeaconCommittees(data.slot, committeeIndices);
974
-
975
- // Create a new Uint32Array to flatten `validatorsByCommittee`
976
- const totalLength = validatorsByCommittee.reduce((acc, curr) => acc + curr.length, 0);
977
- const committeeValidators = new Uint32Array(totalLength);
978
-
979
- let offset = 0;
980
- for (const committee of validatorsByCommittee) {
981
- committeeValidators.set(committee, offset);
982
- offset += committee.length;
983
- }
984
-
985
- return aggregationBits.intersectValues(committeeValidators);
864
+ const shuffling = this.getShufflingAtSlot(attestation.data.slot);
865
+ return getAttestingIndices(shuffling, fork, attestation);
986
866
  }
987
867
 
988
868
  getCommitteeAssignments(
@@ -993,38 +873,6 @@ export class EpochCache {
993
873
  return calculateCommitteeAssignments(shuffling, requestedValidatorIndices);
994
874
  }
995
875
 
996
- /**
997
- * Return the committee assignment in the ``epoch`` for ``validator_index``.
998
- * ``assignment`` returned is a tuple of the following form:
999
- * ``assignment[0]`` is the list of validators in the committee
1000
- * ``assignment[1]`` is the index to which the committee is assigned
1001
- * ``assignment[2]`` is the slot at which the committee is assigned
1002
- * Return null if no assignment..
1003
- */
1004
- getCommitteeAssignment(epoch: Epoch, validatorIndex: ValidatorIndex): phase0.CommitteeAssignment | null {
1005
- if (epoch > this.currentShuffling.epoch + 1) {
1006
- throw Error(
1007
- `Requesting committee assignment for more than 1 epoch ahead: ${epoch} > ${this.currentShuffling.epoch} + 1`
1008
- );
1009
- }
1010
-
1011
- const epochStartSlot = computeStartSlotAtEpoch(epoch);
1012
- const committeeCountPerSlot = this.getCommitteeCountPerSlot(epoch);
1013
- for (let slot = epochStartSlot; slot < epochStartSlot + SLOTS_PER_EPOCH; slot++) {
1014
- for (let i = 0; i < committeeCountPerSlot; i++) {
1015
- const committee = this.getBeaconCommittee(slot, i);
1016
- if (committee.includes(validatorIndex)) {
1017
- return {
1018
- validators: Array.from(committee),
1019
- committeeIndex: i,
1020
- slot,
1021
- };
1022
- }
1023
- }
1024
- }
1025
- return null;
1026
- }
1027
-
1028
876
  isAggregator(slot: Slot, index: CommitteeIndex, slotSignature: BLSSignature): boolean {
1029
877
  const committee = this.getBeaconCommittee(slot, index);
1030
878
  return isAggregatorFromCommitteeLength(committee.length, slotSignature);
@@ -1100,10 +948,6 @@ export class EpochCache {
1100
948
  case this.epoch:
1101
949
  return this.currentShuffling;
1102
950
  case this.nextEpoch:
1103
- if (!this.nextShuffling) {
1104
- this.nextShuffling =
1105
- this.shufflingCache?.getSync(this.nextEpoch, this.getShufflingDecisionRoot(this.nextEpoch)) ?? null;
1106
- }
1107
951
  return this.nextShuffling;
1108
952
  default:
1109
953
  return null;
@@ -1213,7 +1057,6 @@ function getEffectiveBalanceIncrementsByteLen(validatorCount: number): number {
1213
1057
  }
1214
1058
 
1215
1059
  export enum EpochCacheErrorCode {
1216
- COMMITTEE_INDEX_OUT_OF_RANGE = "EPOCH_CONTEXT_ERROR_COMMITTEE_INDEX_OUT_OF_RANGE",
1217
1060
  COMMITTEE_EPOCH_OUT_OF_RANGE = "EPOCH_CONTEXT_ERROR_COMMITTEE_EPOCH_OUT_OF_RANGE",
1218
1061
  DECISION_ROOT_EPOCH_OUT_OF_RANGE = "EPOCH_CONTEXT_ERROR_DECISION_ROOT_EPOCH_OUT_OF_RANGE",
1219
1062
  NEXT_SHUFFLING_NOT_AVAILABLE = "EPOCH_CONTEXT_ERROR_NEXT_SHUFFLING_NOT_AVAILABLE",
@@ -1222,11 +1065,6 @@ export enum EpochCacheErrorCode {
1222
1065
  }
1223
1066
 
1224
1067
  type EpochCacheErrorType =
1225
- | {
1226
- code: EpochCacheErrorCode.COMMITTEE_INDEX_OUT_OF_RANGE;
1227
- index: number;
1228
- maxIndex: number;
1229
- }
1230
1068
  | {
1231
1069
  code: EpochCacheErrorCode.COMMITTEE_EPOCH_OUT_OF_RANGE;
1232
1070
  requestedEpoch: Epoch;
@@ -1,12 +1,6 @@
1
- import {
2
- 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,
@@ -180,7 +180,7 @@ export function loadCachedBeaconState<T extends BeaconStateAllForks & BeaconStat
180
180
  stateBytes,
181
181
  seedValidatorsBytes
182
182
  );
183
- const {pubkey2index, index2pubkey, shufflingCache} = cachedSeedState.epochCtx;
183
+ const {pubkey2index, index2pubkey} = cachedSeedState.epochCtx;
184
184
  // Get the validators sub tree once for all the loop
185
185
  const validators = migratedState.validators;
186
186
  for (const validatorIndex of modifiedValidators) {
@@ -196,7 +196,6 @@ export function loadCachedBeaconState<T extends BeaconStateAllForks & BeaconStat
196
196
  config: cachedSeedState.config,
197
197
  pubkey2index,
198
198
  index2pubkey,
199
- shufflingCache,
200
199
  },
201
200
  {...(opts ?? {}), ...{skipSyncPubkeys: true}}
202
201
  ) as T;
@@ -1,7 +1,6 @@
1
1
  import {SLOTS_PER_EPOCH} from "@lodestar/params";
2
2
  import {ssz} from "@lodestar/types";
3
3
  import {CachedBeaconStateGloas} from "../types.ts";
4
- import {computeExitEpochAndUpdateChurn} from "../util/epoch.ts";
5
4
  import {getBuilderPaymentQuorumThreshold} from "../util/gloas.ts";
6
5
 
7
6
  /**
@@ -12,10 +11,7 @@ export function processBuilderPendingPayments(state: CachedBeaconStateGloas): vo
12
11
 
13
12
  for (let i = 0; i < SLOTS_PER_EPOCH; i++) {
14
13
  const payment = state.builderPendingPayments.get(i);
15
- if (payment.weight > quorum) {
16
- const exitQueueEpoch = computeExitEpochAndUpdateChurn(state, BigInt(payment.withdrawal.amount));
17
- payment.withdrawal.withdrawableEpoch = exitQueueEpoch + state.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY;
18
-
14
+ if (payment.weight >= quorum) {
19
15
  state.builderPendingWithdrawals.push(payment.withdrawal);
20
16
  }
21
17
  }
@@ -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,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";
@@ -7,6 +7,7 @@ import {
7
7
  } from "@lodestar/params";
8
8
  import {BeaconBlock, altair, phase0, rewards} from "@lodestar/types";
9
9
  import {processAttestationsAltair} from "../block/processAttestationsAltair.js";
10
+ import {RewardCache} from "../cache/rewardCache.js";
10
11
  import {CachedBeaconStateAllForks, CachedBeaconStateAltair, CachedBeaconStatePhase0} from "../cache/stateCache.js";
11
12
  import {getAttesterSlashableIndices} from "../util/attestation.js";
12
13
 
@@ -23,12 +24,14 @@ type SubRewardValue = number; // All reward values should be integer
23
24
  export async function computeBlockRewards(
24
25
  config: BeaconConfig,
25
26
  block: BeaconBlock,
26
- preState: CachedBeaconStateAllForks,
27
- postState?: CachedBeaconStateAllForks
27
+ preStateIn: CachedBeaconStateAllForks,
28
+ proposerRewards?: RewardCache
28
29
  ): Promise<rewards.BlockRewards> {
30
+ const preState = preStateIn.clone();
31
+
29
32
  const fork = config.getForkName(block.slot);
30
33
  const {attestations: cachedAttestationsReward = 0, syncAggregate: cachedSyncAggregateReward = 0} =
31
- postState?.proposerRewards ?? {};
34
+ proposerRewards ?? {};
32
35
  let blockAttestationReward = cachedAttestationsReward;
33
36
  let syncAggregateReward = cachedSyncAggregateReward;
34
37