@lodestar/state-transition 1.38.0-dev.bc1fed4d3d → 1.38.0-dev.f231d7e8ff

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 (134) hide show
  1. package/lib/block/index.d.ts +4 -1
  2. package/lib/block/index.d.ts.map +1 -1
  3. package/lib/block/index.js +19 -8
  4. package/lib/block/index.js.map +1 -1
  5. package/lib/block/isValidIndexedPayloadAttestation.d.ts +4 -0
  6. package/lib/block/isValidIndexedPayloadAttestation.d.ts.map +1 -0
  7. package/lib/block/isValidIndexedPayloadAttestation.js +14 -0
  8. package/lib/block/isValidIndexedPayloadAttestation.js.map +1 -0
  9. package/lib/block/processAttestationPhase0.d.ts.map +1 -1
  10. package/lib/block/processAttestationPhase0.js +6 -1
  11. package/lib/block/processAttestationPhase0.js.map +1 -1
  12. package/lib/block/processAttestationsAltair.d.ts +3 -3
  13. package/lib/block/processAttestationsAltair.d.ts.map +1 -1
  14. package/lib/block/processAttestationsAltair.js +46 -4
  15. package/lib/block/processAttestationsAltair.js.map +1 -1
  16. package/lib/block/processConsolidationRequest.d.ts +3 -2
  17. package/lib/block/processConsolidationRequest.d.ts.map +1 -1
  18. package/lib/block/processConsolidationRequest.js +2 -2
  19. package/lib/block/processConsolidationRequest.js.map +1 -1
  20. package/lib/block/processDepositRequest.d.ts +2 -2
  21. package/lib/block/processDepositRequest.d.ts.map +1 -1
  22. package/lib/block/processDepositRequest.js.map +1 -1
  23. package/lib/block/processExecutionPayloadBid.d.ts +5 -0
  24. package/lib/block/processExecutionPayloadBid.d.ts.map +1 -0
  25. package/lib/block/processExecutionPayloadBid.js +89 -0
  26. package/lib/block/processExecutionPayloadBid.js.map +1 -0
  27. package/lib/block/processExecutionPayloadEnvelope.d.ts +4 -0
  28. package/lib/block/processExecutionPayloadEnvelope.d.ts.map +1 -0
  29. package/lib/block/processExecutionPayloadEnvelope.js +118 -0
  30. package/lib/block/processExecutionPayloadEnvelope.js.map +1 -0
  31. package/lib/block/processOperations.d.ts.map +1 -1
  32. package/lib/block/processOperations.js +8 -2
  33. package/lib/block/processOperations.js.map +1 -1
  34. package/lib/block/processPayloadAttestation.d.ts +4 -0
  35. package/lib/block/processPayloadAttestation.d.ts.map +1 -0
  36. package/lib/block/processPayloadAttestation.js +16 -0
  37. package/lib/block/processPayloadAttestation.js.map +1 -0
  38. package/lib/block/processProposerSlashing.d.ts.map +1 -1
  39. package/lib/block/processProposerSlashing.js +16 -1
  40. package/lib/block/processProposerSlashing.js.map +1 -1
  41. package/lib/block/processVoluntaryExit.js +1 -1
  42. package/lib/block/processVoluntaryExit.js.map +1 -1
  43. package/lib/block/processWithdrawalRequest.d.ts +2 -2
  44. package/lib/block/processWithdrawalRequest.d.ts.map +1 -1
  45. package/lib/block/processWithdrawalRequest.js +1 -1
  46. package/lib/block/processWithdrawalRequest.js.map +1 -1
  47. package/lib/block/processWithdrawals.d.ts +4 -3
  48. package/lib/block/processWithdrawals.d.ts.map +1 -1
  49. package/lib/block/processWithdrawals.js +89 -19
  50. package/lib/block/processWithdrawals.js.map +1 -1
  51. package/lib/cache/epochCache.d.ts +5 -1
  52. package/lib/cache/epochCache.d.ts.map +1 -1
  53. package/lib/cache/epochCache.js +34 -1
  54. package/lib/cache/epochCache.js.map +1 -1
  55. package/lib/epoch/index.d.ts +4 -2
  56. package/lib/epoch/index.d.ts.map +1 -1
  57. package/lib/epoch/index.js +10 -1
  58. package/lib/epoch/index.js.map +1 -1
  59. package/lib/epoch/processBuilderPendingPayments.d.ts +6 -0
  60. package/lib/epoch/processBuilderPendingPayments.d.ts.map +1 -0
  61. package/lib/epoch/processBuilderPendingPayments.js +28 -0
  62. package/lib/epoch/processBuilderPendingPayments.js.map +1 -0
  63. package/lib/index.d.ts +1 -1
  64. package/lib/index.d.ts.map +1 -1
  65. package/lib/index.js.map +1 -1
  66. package/lib/signatureSets/index.d.ts +1 -0
  67. package/lib/signatureSets/index.d.ts.map +1 -1
  68. package/lib/signatureSets/index.js +1 -0
  69. package/lib/signatureSets/index.js.map +1 -1
  70. package/lib/signatureSets/indexedPayloadAttestation.d.ts +6 -0
  71. package/lib/signatureSets/indexedPayloadAttestation.d.ts.map +1 -0
  72. package/lib/signatureSets/indexedPayloadAttestation.js +11 -0
  73. package/lib/signatureSets/indexedPayloadAttestation.js.map +1 -0
  74. package/lib/slot/index.d.ts +2 -1
  75. package/lib/slot/index.d.ts.map +1 -1
  76. package/lib/slot/index.js +6 -2
  77. package/lib/slot/index.js.map +1 -1
  78. package/lib/slot/upgradeStateToAltair.js +1 -1
  79. package/lib/slot/upgradeStateToAltair.js.map +1 -1
  80. package/lib/slot/upgradeStateToGloas.d.ts +0 -1
  81. package/lib/slot/upgradeStateToGloas.d.ts.map +1 -1
  82. package/lib/slot/upgradeStateToGloas.js +47 -5
  83. package/lib/slot/upgradeStateToGloas.js.map +1 -1
  84. package/lib/stateTransition.js +4 -3
  85. package/lib/stateTransition.js.map +1 -1
  86. package/lib/util/electra.d.ts +5 -5
  87. package/lib/util/electra.d.ts.map +1 -1
  88. package/lib/util/electra.js +2 -1
  89. package/lib/util/electra.js.map +1 -1
  90. package/lib/util/epoch.d.ts +3 -3
  91. package/lib/util/epoch.d.ts.map +1 -1
  92. package/lib/util/epoch.js.map +1 -1
  93. package/lib/util/gloas.d.ts +11 -0
  94. package/lib/util/gloas.d.ts.map +1 -0
  95. package/lib/util/gloas.js +35 -0
  96. package/lib/util/gloas.js.map +1 -0
  97. package/lib/util/seed.d.ts +5 -1
  98. package/lib/util/seed.d.ts.map +1 -1
  99. package/lib/util/seed.js +33 -1
  100. package/lib/util/seed.js.map +1 -1
  101. package/lib/util/validator.d.ts +2 -2
  102. package/lib/util/validator.d.ts.map +1 -1
  103. package/lib/util/validator.js +14 -1
  104. package/lib/util/validator.js.map +1 -1
  105. package/package.json +6 -6
  106. package/src/block/index.ts +35 -14
  107. package/src/block/isValidIndexedPayloadAttestation.ts +23 -0
  108. package/src/block/processAttestationPhase0.ts +5 -1
  109. package/src/block/processAttestationsAltair.ts +62 -5
  110. package/src/block/processConsolidationRequest.ts +6 -5
  111. package/src/block/processDepositRequest.ts +5 -2
  112. package/src/block/processExecutionPayloadBid.ts +120 -0
  113. package/src/block/processExecutionPayloadEnvelope.ts +181 -0
  114. package/src/block/processOperations.ts +16 -4
  115. package/src/block/processPayloadAttestation.ts +25 -0
  116. package/src/block/processProposerSlashing.ts +24 -3
  117. package/src/block/processVoluntaryExit.ts +1 -1
  118. package/src/block/processWithdrawalRequest.ts +4 -4
  119. package/src/block/processWithdrawals.ts +118 -27
  120. package/src/cache/epochCache.ts +58 -1
  121. package/src/epoch/index.ts +12 -0
  122. package/src/epoch/processBuilderPendingPayments.ts +31 -0
  123. package/src/index.ts +2 -0
  124. package/src/signatureSets/index.ts +1 -0
  125. package/src/signatureSets/indexedPayloadAttestation.ts +24 -0
  126. package/src/slot/index.ts +11 -3
  127. package/src/slot/upgradeStateToAltair.ts +2 -1
  128. package/src/slot/upgradeStateToGloas.ts +49 -5
  129. package/src/stateTransition.ts +4 -4
  130. package/src/util/electra.ts +15 -6
  131. package/src/util/epoch.ts +6 -3
  132. package/src/util/gloas.ts +58 -0
  133. package/src/util/seed.ts +57 -1
  134. package/src/util/validator.ts +21 -2
@@ -1,24 +1,68 @@
1
+ import {SLOTS_PER_HISTORICAL_ROOT} from "@lodestar/params";
1
2
  import {ssz} from "@lodestar/types";
2
3
  import {getCachedBeaconState} from "../cache/stateCache.js";
3
4
  import {CachedBeaconStateFulu, CachedBeaconStateGloas} from "../types.js";
4
5
 
5
6
  /**
6
7
  * Upgrade a state from Fulu to Gloas.
7
- * TODO GLOAS: Implement this
8
8
  */
9
9
  export function upgradeStateToGloas(stateFulu: CachedBeaconStateFulu): CachedBeaconStateGloas {
10
10
  const {config} = stateFulu;
11
11
 
12
- const stateFuluNode = ssz.fulu.BeaconState.commitViewDU(stateFulu);
13
- const stateGloasView = ssz.gloas.BeaconState.getViewDU(stateFuluNode);
12
+ ssz.fulu.BeaconState.commitViewDU(stateFulu);
13
+ const stateGloasCloned = stateFulu;
14
14
 
15
- const stateGloas = getCachedBeaconState(stateGloasView, stateFulu);
15
+ const stateGloasView = ssz.gloas.BeaconState.defaultViewDU();
16
16
 
17
- stateGloas.fork = ssz.phase0.Fork.toViewDU({
17
+ stateGloasView.genesisTime = stateGloasCloned.genesisTime;
18
+ stateGloasView.genesisValidatorsRoot = stateGloasCloned.genesisValidatorsRoot;
19
+ stateGloasView.slot = stateGloasCloned.slot;
20
+ stateGloasView.fork = ssz.phase0.Fork.toViewDU({
18
21
  previousVersion: stateFulu.fork.currentVersion,
19
22
  currentVersion: config.GLOAS_FORK_VERSION,
20
23
  epoch: stateFulu.epochCtx.epoch,
21
24
  });
25
+ stateGloasView.latestBlockHeader = stateGloasCloned.latestBlockHeader;
26
+ stateGloasView.blockRoots = stateGloasCloned.blockRoots;
27
+ stateGloasView.stateRoots = stateGloasCloned.stateRoots;
28
+ stateGloasView.historicalRoots = stateGloasCloned.historicalRoots;
29
+ stateGloasView.eth1Data = stateGloasCloned.eth1Data;
30
+ stateGloasView.eth1DataVotes = stateGloasCloned.eth1DataVotes;
31
+ stateGloasView.eth1DepositIndex = stateGloasCloned.eth1DepositIndex;
32
+ stateGloasView.validators = stateGloasCloned.validators;
33
+ stateGloasView.balances = stateGloasCloned.balances;
34
+ stateGloasView.randaoMixes = stateGloasCloned.randaoMixes;
35
+ stateGloasView.slashings = stateGloasCloned.slashings;
36
+ stateGloasView.previousEpochParticipation = stateGloasCloned.previousEpochParticipation;
37
+ stateGloasView.currentEpochParticipation = stateGloasCloned.currentEpochParticipation;
38
+ stateGloasView.justificationBits = stateGloasCloned.justificationBits;
39
+ stateGloasView.previousJustifiedCheckpoint = stateGloasCloned.previousJustifiedCheckpoint;
40
+ stateGloasView.currentJustifiedCheckpoint = stateGloasCloned.currentJustifiedCheckpoint;
41
+ stateGloasView.finalizedCheckpoint = stateGloasCloned.finalizedCheckpoint;
42
+ stateGloasView.inactivityScores = stateGloasCloned.inactivityScores;
43
+ stateGloasView.currentSyncCommittee = stateGloasCloned.currentSyncCommittee;
44
+ stateGloasView.nextSyncCommittee = stateGloasCloned.nextSyncCommittee;
45
+ stateGloasView.latestExecutionPayloadBid.blockHash = stateFulu.latestExecutionPayloadHeader.blockHash;
46
+ stateGloasView.nextWithdrawalIndex = stateGloasCloned.nextWithdrawalIndex;
47
+ stateGloasView.nextWithdrawalValidatorIndex = stateGloasCloned.nextWithdrawalValidatorIndex;
48
+ stateGloasView.historicalSummaries = stateGloasCloned.historicalSummaries;
49
+ stateGloasView.depositRequestsStartIndex = stateGloasCloned.depositRequestsStartIndex;
50
+ stateGloasView.depositBalanceToConsume = stateGloasCloned.depositBalanceToConsume;
51
+ stateGloasView.exitBalanceToConsume = stateGloasCloned.exitBalanceToConsume;
52
+ stateGloasView.earliestExitEpoch = stateGloasCloned.earliestExitEpoch;
53
+ stateGloasView.consolidationBalanceToConsume = stateGloasCloned.consolidationBalanceToConsume;
54
+ stateGloasView.earliestConsolidationEpoch = stateGloasCloned.earliestConsolidationEpoch;
55
+ stateGloasView.pendingDeposits = stateGloasCloned.pendingDeposits;
56
+ stateGloasView.pendingPartialWithdrawals = stateGloasCloned.pendingPartialWithdrawals;
57
+ stateGloasView.pendingConsolidations = stateGloasCloned.pendingConsolidations;
58
+ stateGloasView.proposerLookahead = stateGloasCloned.proposerLookahead;
59
+
60
+ for (let i = 0; i < SLOTS_PER_HISTORICAL_ROOT; i++) {
61
+ stateGloasView.executionPayloadAvailability.set(i, true);
62
+ }
63
+ stateGloasView.latestBlockHash = stateFulu.latestExecutionPayloadHeader.blockHash;
64
+
65
+ const stateGloas = getCachedBeaconState(stateGloasView, stateFulu);
22
66
 
23
67
  stateGloas.commit();
24
68
  // Clear cache to ensure the cache of fulu fields is not used by new gloas fields
@@ -217,13 +217,13 @@ function processSlotsWithTransientCache(
217
217
  }
218
218
 
219
219
  while (postState.slot < slot) {
220
- processSlot(postState);
220
+ const fork = postState.config.getForkSeq(postState.slot);
221
+ processSlot(fork, postState);
221
222
 
222
223
  // Process epoch on the first slot of the next epoch
224
+ // We use `fork` because at fork boundary we don't want to process
225
+ // "next fork" epoch before upgrading state
223
226
  if ((postState.slot + 1) % SLOTS_PER_EPOCH === 0) {
224
- // At fork boundary we don't want to process "next fork" epoch before upgrading state
225
- const fork = postState.config.getForkSeq(postState.slot);
226
-
227
227
  const epochTransitionTimer = metrics?.epochTransitionTime.startTimer();
228
228
 
229
229
  let epochTransitionCache: EpochTransitionCache;
@@ -1,11 +1,14 @@
1
1
  import {COMPOUNDING_WITHDRAWAL_PREFIX, GENESIS_SLOT, MIN_ACTIVATION_BALANCE} from "@lodestar/params";
2
2
  import {ValidatorIndex, ssz} from "@lodestar/types";
3
3
  import {G2_POINT_AT_INFINITY} from "../constants/constants.js";
4
- import {CachedBeaconStateElectra} from "../types.js";
4
+ import {CachedBeaconStateElectra, CachedBeaconStateGloas} from "../types.js";
5
5
  import {hasEth1WithdrawalCredential} from "./capella.js";
6
+ import {hasBuilderWithdrawalCredential} from "./gloas.ts";
6
7
 
7
8
  export function hasCompoundingWithdrawalCredential(withdrawalCredentials: Uint8Array): boolean {
8
- return withdrawalCredentials[0] === COMPOUNDING_WITHDRAWAL_PREFIX;
9
+ return (
10
+ withdrawalCredentials[0] === COMPOUNDING_WITHDRAWAL_PREFIX || hasBuilderWithdrawalCredential(withdrawalCredentials)
11
+ );
9
12
  }
10
13
 
11
14
  export function hasExecutionWithdrawalCredential(withdrawalCredentials: Uint8Array): boolean {
@@ -14,7 +17,10 @@ export function hasExecutionWithdrawalCredential(withdrawalCredentials: Uint8Arr
14
17
  );
15
18
  }
16
19
 
17
- export function switchToCompoundingValidator(state: CachedBeaconStateElectra, index: ValidatorIndex): void {
20
+ export function switchToCompoundingValidator(
21
+ state: CachedBeaconStateElectra | CachedBeaconStateGloas,
22
+ index: ValidatorIndex
23
+ ): void {
18
24
  const validator = state.validators.get(index);
19
25
 
20
26
  // directly modifying the byte leads to ssz missing the modification resulting into
@@ -30,7 +36,10 @@ export function switchToCompoundingValidator(state: CachedBeaconStateElectra, in
30
36
  queueExcessActiveBalance(state, index);
31
37
  }
32
38
 
33
- export function queueExcessActiveBalance(state: CachedBeaconStateElectra, index: ValidatorIndex): void {
39
+ export function queueExcessActiveBalance(
40
+ state: CachedBeaconStateElectra | CachedBeaconStateGloas,
41
+ index: ValidatorIndex
42
+ ): void {
34
43
  const balance = state.balances.get(index);
35
44
  if (balance > MIN_ACTIVATION_BALANCE) {
36
45
  const validator = state.validators.getReadonly(index);
@@ -53,7 +62,7 @@ export function queueExcessActiveBalance(state: CachedBeaconStateElectra, index:
53
62
  /**
54
63
  * Since we share pubkey2index, pubkey maybe added by other epoch transition but we don't have that validator in this state
55
64
  */
56
- export function isPubkeyKnown(state: CachedBeaconStateElectra, pubkey: Uint8Array): boolean {
65
+ export function isPubkeyKnown(state: CachedBeaconStateElectra | CachedBeaconStateGloas, pubkey: Uint8Array): boolean {
57
66
  return isValidatorKnown(state, state.epochCtx.getValidatorIndex(pubkey));
58
67
  }
59
68
 
@@ -61,7 +70,7 @@ export function isPubkeyKnown(state: CachedBeaconStateElectra, pubkey: Uint8Arra
61
70
  * Since we share pubkey2index, validatorIndex maybe not null but we don't have that validator in this state
62
71
  */
63
72
  export function isValidatorKnown(
64
- state: CachedBeaconStateElectra,
73
+ state: CachedBeaconStateElectra | CachedBeaconStateGloas,
65
74
  index: ValidatorIndex | null
66
75
  ): index is ValidatorIndex {
67
76
  return index !== null && index < state.validators.length;
package/src/util/epoch.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import {EPOCHS_PER_SYNC_COMMITTEE_PERIOD, GENESIS_EPOCH, MAX_SEED_LOOKAHEAD, SLOTS_PER_EPOCH} from "@lodestar/params";
2
2
  import {BeaconState, Epoch, Gwei, Slot, SyncPeriod} from "@lodestar/types";
3
- import {CachedBeaconStateElectra} from "../types.js";
3
+ import {CachedBeaconStateElectra, CachedBeaconStateGloas} from "../types.js";
4
4
  import {getActivationExitChurnLimit, getConsolidationChurnLimit} from "./validator.js";
5
5
 
6
6
  /**
@@ -41,7 +41,10 @@ export function computeActivationExitEpoch(epoch: Epoch): Epoch {
41
41
  return epoch + 1 + MAX_SEED_LOOKAHEAD;
42
42
  }
43
43
 
44
- export function computeExitEpochAndUpdateChurn(state: CachedBeaconStateElectra, exitBalance: Gwei): number {
44
+ export function computeExitEpochAndUpdateChurn(
45
+ state: CachedBeaconStateElectra | CachedBeaconStateGloas,
46
+ exitBalance: Gwei
47
+ ): number {
45
48
  let earliestExitEpoch = Math.max(state.earliestExitEpoch, computeActivationExitEpoch(state.epochCtx.epoch));
46
49
  const perEpochChurn = getActivationExitChurnLimit(state.epochCtx);
47
50
 
@@ -65,7 +68,7 @@ export function computeExitEpochAndUpdateChurn(state: CachedBeaconStateElectra,
65
68
  }
66
69
 
67
70
  export function computeConsolidationEpochAndUpdateChurn(
68
- state: CachedBeaconStateElectra,
71
+ state: CachedBeaconStateElectra | CachedBeaconStateGloas,
69
72
  consolidationBalance: Gwei
70
73
  ): number {
71
74
  let earliestConsolidationEpoch = Math.max(
@@ -0,0 +1,58 @@
1
+ import {byteArrayEquals} from "@chainsafe/ssz";
2
+ import {
3
+ BUILDER_PAYMENT_THRESHOLD_DENOMINATOR,
4
+ BUILDER_PAYMENT_THRESHOLD_NUMERATOR,
5
+ BUILDER_WITHDRAWAL_PREFIX,
6
+ EFFECTIVE_BALANCE_INCREMENT,
7
+ SLOTS_PER_EPOCH,
8
+ } from "@lodestar/params";
9
+ import {gloas} from "@lodestar/types";
10
+ import {AttestationData} from "@lodestar/types/phase0";
11
+ import {CachedBeaconStateGloas} from "../types.ts";
12
+ import {getBlockRootAtSlot} from "./blockRoot.ts";
13
+ import {computeEpochAtSlot} from "./epoch.ts";
14
+ import {RootCache} from "./rootCache.ts";
15
+
16
+ export function hasBuilderWithdrawalCredential(withdrawalCredentials: Uint8Array): boolean {
17
+ return withdrawalCredentials[0] === BUILDER_WITHDRAWAL_PREFIX;
18
+ }
19
+
20
+ export function getBuilderPaymentQuorumThreshold(state: CachedBeaconStateGloas): number {
21
+ const quorum =
22
+ Math.floor((state.epochCtx.totalActiveBalanceIncrements * EFFECTIVE_BALANCE_INCREMENT) / SLOTS_PER_EPOCH) *
23
+ BUILDER_PAYMENT_THRESHOLD_NUMERATOR;
24
+
25
+ return Math.floor(quorum / BUILDER_PAYMENT_THRESHOLD_DENOMINATOR);
26
+ }
27
+
28
+ export function isBuilderPaymentWithdrawable(
29
+ state: CachedBeaconStateGloas,
30
+ withdrawal: gloas.BuilderPendingWithdrawal
31
+ ): boolean {
32
+ const builder = state.validators.getReadonly(withdrawal.builderIndex);
33
+ const currentEpoch = computeEpochAtSlot(state.slot);
34
+
35
+ return builder.withdrawableEpoch >= currentEpoch || !builder.slashed;
36
+ }
37
+
38
+ export function isAttestationSameSlot(state: CachedBeaconStateGloas, data: AttestationData): boolean {
39
+ if (data.slot === 0) return true;
40
+
41
+ const isMatchingBlockRoot = byteArrayEquals(data.beaconBlockRoot, getBlockRootAtSlot(state, data.slot));
42
+ const isCurrentBlockRoot = !byteArrayEquals(data.beaconBlockRoot, getBlockRootAtSlot(state, data.slot - 1));
43
+
44
+ return isMatchingBlockRoot && isCurrentBlockRoot;
45
+ }
46
+
47
+ export function isAttestationSameSlotRootCache(rootCache: RootCache, data: AttestationData): boolean {
48
+ if (data.slot === 0) return true;
49
+
50
+ const isMatchingBlockRoot = byteArrayEquals(data.beaconBlockRoot, rootCache.getBlockRootAtSlot(data.slot));
51
+ const isCurrentBlockRoot = !byteArrayEquals(data.beaconBlockRoot, rootCache.getBlockRootAtSlot(data.slot - 1));
52
+
53
+ return isMatchingBlockRoot && isCurrentBlockRoot;
54
+ }
55
+
56
+ export function isParentBlockFull(state: CachedBeaconStateGloas): boolean {
57
+ return byteArrayEquals(state.latestExecutionPayloadBid.blockHash, state.latestBlockHash);
58
+ }
package/src/util/seed.ts CHANGED
@@ -5,6 +5,7 @@ import {
5
5
  } from "@chainsafe/swap-or-not-shuffle";
6
6
  import {
7
7
  DOMAIN_BEACON_PROPOSER,
8
+ DOMAIN_PTC_ATTESTER,
8
9
  DOMAIN_SYNC_COMMITTEE,
9
10
  EFFECTIVE_BALANCE_INCREMENT,
10
11
  EPOCHS_PER_HISTORICAL_VECTOR,
@@ -12,6 +13,7 @@ import {
12
13
  MAX_EFFECTIVE_BALANCE,
13
14
  MAX_EFFECTIVE_BALANCE_ELECTRA,
14
15
  MIN_SEED_LOOKAHEAD,
16
+ PTC_SIZE,
15
17
  SHUFFLE_ROUND_COUNT,
16
18
  SLOTS_PER_EPOCH,
17
19
  SYNC_COMMITTEE_SIZE,
@@ -19,7 +21,7 @@ import {
19
21
  import {Bytes32, DomainType, Epoch, ValidatorIndex} from "@lodestar/types";
20
22
  import {assert, bytesToBigInt, bytesToInt, intToBytes} from "@lodestar/utils";
21
23
  import {EffectiveBalanceIncrements} from "../cache/effectiveBalanceIncrements.js";
22
- import {BeaconStateAllForks, CachedBeaconStateAllForks} from "../types.js";
24
+ import {BeaconStateAllForks, BeaconStateGloas, CachedBeaconStateAllForks} from "../types.js";
23
25
  import {computeEpochAtSlot, computeStartSlotAtEpoch} from "./epoch.js";
24
26
 
25
27
  /**
@@ -266,6 +268,60 @@ export function getNextSyncCommitteeIndices(
266
268
  );
267
269
  }
268
270
 
271
+ export function naiveGetPayloadTimlinessCommitteeIndices(
272
+ state: BeaconStateGloas,
273
+ shuffling: {committees: Uint32Array[][]},
274
+ effectiveBalanceIncrements: EffectiveBalanceIncrements,
275
+ epoch: Epoch
276
+ ): ValidatorIndex[][] {
277
+ const epochSeed = getSeed(state, epoch, DOMAIN_PTC_ATTESTER);
278
+ const startSlot = computeStartSlotAtEpoch(epoch);
279
+ const committeeIndices = [];
280
+
281
+ for (let slot = startSlot; slot < startSlot + SLOTS_PER_EPOCH; slot++) {
282
+ const slotCommittees = shuffling.committees[slot % SLOTS_PER_EPOCH];
283
+ const indices = naiveComputePayloadTimelinessCommitteeIndices(
284
+ effectiveBalanceIncrements,
285
+ slotCommittees.flatMap((c) => Array.from(c)),
286
+ digest(Buffer.concat([epochSeed, intToBytes(slot, 8)]))
287
+ );
288
+ committeeIndices.push(indices);
289
+ }
290
+
291
+ return committeeIndices;
292
+ }
293
+
294
+ export function naiveComputePayloadTimelinessCommitteeIndices(
295
+ effectiveBalanceIncrements: EffectiveBalanceIncrements,
296
+ indices: ArrayLike<ValidatorIndex>,
297
+ seed: Uint8Array
298
+ ): ValidatorIndex[] {
299
+ if (indices.length === 0) {
300
+ throw Error("Validator indices must not be empty");
301
+ }
302
+
303
+ const result = [];
304
+
305
+ const MAX_RANDOM_VALUE = 2 ** 16 - 1;
306
+ const MAX_EFFECTIVE_BALANCE_INCREMENT = MAX_EFFECTIVE_BALANCE_ELECTRA / EFFECTIVE_BALANCE_INCREMENT;
307
+
308
+ let i = 0;
309
+ while (result.length < PTC_SIZE) {
310
+ const candidateIndex = indices[i % indices.length];
311
+ const randomBytes = digest(Buffer.concat([seed, intToBytes(Math.floor(i / 16), 8, "le")]));
312
+ const offset = (i % 16) * 2;
313
+ const randomValue = bytesToInt(randomBytes.subarray(offset, offset + 2));
314
+
315
+ const effectiveBalanceIncrement = effectiveBalanceIncrements[candidateIndex];
316
+ if (effectiveBalanceIncrement * MAX_RANDOM_VALUE >= MAX_EFFECTIVE_BALANCE_INCREMENT * randomValue) {
317
+ result.push(candidateIndex);
318
+ }
319
+ i += 1;
320
+ }
321
+
322
+ return result;
323
+ }
324
+
269
325
  /**
270
326
  * Return the shuffled validator index corresponding to ``seed`` (and ``index_count``).
271
327
  *
@@ -7,7 +7,7 @@ import {
7
7
  } from "@lodestar/params";
8
8
  import {Epoch, ValidatorIndex, phase0} from "@lodestar/types";
9
9
  import {intDiv} from "@lodestar/utils";
10
- import {BeaconStateAllForks, CachedBeaconStateElectra, EpochCache} from "../types.js";
10
+ import {BeaconStateAllForks, CachedBeaconStateElectra, CachedBeaconStateGloas, EpochCache} from "../types.js";
11
11
  import {hasCompoundingWithdrawalCredential} from "./electra.js";
12
12
 
13
13
  /**
@@ -94,12 +94,31 @@ export function getMaxEffectiveBalance(withdrawalCredentials: Uint8Array): numbe
94
94
  return MIN_ACTIVATION_BALANCE;
95
95
  }
96
96
 
97
- export function getPendingBalanceToWithdraw(state: CachedBeaconStateElectra, validatorIndex: ValidatorIndex): number {
97
+ export function getPendingBalanceToWithdraw(
98
+ fork: ForkSeq,
99
+ state: CachedBeaconStateElectra | CachedBeaconStateGloas,
100
+ validatorIndex: ValidatorIndex
101
+ ): number {
98
102
  let total = 0;
99
103
  for (const item of state.pendingPartialWithdrawals.getAllReadonly()) {
100
104
  if (item.validatorIndex === validatorIndex) {
101
105
  total += Number(item.amount);
102
106
  }
103
107
  }
108
+
109
+ if (fork >= ForkSeq.gloas) {
110
+ const stateGloas = state as CachedBeaconStateGloas;
111
+ for (const item of stateGloas.builderPendingWithdrawals.getAllReadonly()) {
112
+ if (item.builderIndex === validatorIndex) {
113
+ total += item.amount;
114
+ }
115
+ }
116
+ for (const item of stateGloas.builderPendingPayments.getAllReadonly()) {
117
+ if (item.withdrawal.builderIndex === validatorIndex) {
118
+ total += item.withdrawal.amount;
119
+ }
120
+ }
121
+ }
122
+
104
123
  return total;
105
124
  }