@lodestar/state-transition 1.50.0 → 1.51.0-dev.02c5a19f35

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 (153) hide show
  1. package/lib/block/index.d.ts.map +1 -1
  2. package/lib/block/index.js +1 -1
  3. package/lib/block/index.js.map +1 -1
  4. package/lib/block/processAttestationsAltair.d.ts.map +1 -1
  5. package/lib/block/processAttestationsAltair.js +3 -1
  6. package/lib/block/processAttestationsAltair.js.map +1 -1
  7. package/lib/block/processDeposit.d.ts +2 -2
  8. package/lib/block/processDeposit.d.ts.map +1 -1
  9. package/lib/block/processDeposit.js.map +1 -1
  10. package/lib/block/processEth1Data.d.ts +3 -3
  11. package/lib/block/processEth1Data.d.ts.map +1 -1
  12. package/lib/block/processEth1Data.js.map +1 -1
  13. package/lib/block/processOperations.d.ts.map +1 -1
  14. package/lib/block/processOperations.js +8 -6
  15. package/lib/block/processOperations.js.map +1 -1
  16. package/lib/cache/epochCache.d.ts +3 -0
  17. package/lib/cache/epochCache.d.ts.map +1 -1
  18. package/lib/cache/epochCache.js +11 -4
  19. package/lib/cache/epochCache.js.map +1 -1
  20. package/lib/cache/epochTransitionCache.d.ts.map +1 -1
  21. package/lib/cache/epochTransitionCache.js +2 -1
  22. package/lib/cache/epochTransitionCache.js.map +1 -1
  23. package/lib/cache/stateCache.d.ts +2 -1
  24. package/lib/cache/stateCache.d.ts.map +1 -1
  25. package/lib/cache/stateCache.js.map +1 -1
  26. package/lib/cache/types.d.ts +2 -1
  27. package/lib/cache/types.d.ts.map +1 -1
  28. package/lib/epoch/getRewardsAndPenalties.d.ts.map +1 -1
  29. package/lib/epoch/getRewardsAndPenalties.js +9 -2
  30. package/lib/epoch/getRewardsAndPenalties.js.map +1 -1
  31. package/lib/epoch/index.d.ts.map +1 -1
  32. package/lib/epoch/index.js +3 -1
  33. package/lib/epoch/index.js.map +1 -1
  34. package/lib/epoch/processEth1DataReset.d.ts +2 -2
  35. package/lib/epoch/processEth1DataReset.d.ts.map +1 -1
  36. package/lib/epoch/processEth1DataReset.js.map +1 -1
  37. package/lib/index.d.ts +1 -1
  38. package/lib/index.d.ts.map +1 -1
  39. package/lib/index.js.map +1 -1
  40. package/lib/metrics.d.ts +1 -1
  41. package/lib/metrics.d.ts.map +1 -1
  42. package/lib/metrics.js +4 -4
  43. package/lib/metrics.js.map +1 -1
  44. package/lib/rewards/attestationsRewards.d.ts.map +1 -1
  45. package/lib/rewards/attestationsRewards.js +10 -3
  46. package/lib/rewards/attestationsRewards.js.map +1 -1
  47. package/lib/signatureSets/inclusionList.d.ts +5 -0
  48. package/lib/signatureSets/inclusionList.d.ts.map +1 -0
  49. package/lib/signatureSets/inclusionList.js +14 -0
  50. package/lib/signatureSets/inclusionList.js.map +1 -0
  51. package/lib/signatureSets/index.d.ts +1 -0
  52. package/lib/signatureSets/index.d.ts.map +1 -1
  53. package/lib/signatureSets/index.js +1 -0
  54. package/lib/signatureSets/index.js.map +1 -1
  55. package/lib/slot/upgradeStateToHeze.d.ts.map +1 -1
  56. package/lib/slot/upgradeStateToHeze.js +4 -4
  57. package/lib/slot/upgradeStateToHeze.js.map +1 -1
  58. package/lib/stateTransition.js +2 -2
  59. package/lib/stateTransition.js.map +1 -1
  60. package/lib/stateView/beaconStateView.d.ts +1 -0
  61. package/lib/stateView/beaconStateView.d.ts.map +1 -1
  62. package/lib/stateView/beaconStateView.js +6 -0
  63. package/lib/stateView/beaconStateView.js.map +1 -1
  64. package/lib/stateView/interface.d.ts +4 -1
  65. package/lib/stateView/interface.d.ts.map +1 -1
  66. package/lib/stateView/interface.js.map +1 -1
  67. package/lib/stateView/nativeBeaconStateView.d.ts +2 -0
  68. package/lib/stateView/nativeBeaconStateView.d.ts.map +1 -1
  69. package/lib/stateView/nativeBeaconStateView.js +14 -0
  70. package/lib/stateView/nativeBeaconStateView.js.map +1 -1
  71. package/lib/types.d.ts +2 -2
  72. package/lib/types.d.ts.map +1 -1
  73. package/lib/util/altair.d.ts +3 -1
  74. package/lib/util/altair.d.ts.map +1 -1
  75. package/lib/util/altair.js +7 -4
  76. package/lib/util/altair.js.map +1 -1
  77. package/lib/util/blindedBlock.d.ts +1 -1
  78. package/lib/util/blindedBlock.d.ts.map +1 -1
  79. package/lib/util/blindedBlock.js +5 -2
  80. package/lib/util/blindedBlock.js.map +1 -1
  81. package/lib/util/clock.d.ts +5 -0
  82. package/lib/util/clock.d.ts.map +1 -1
  83. package/lib/util/clock.js +20 -17
  84. package/lib/util/clock.js.map +1 -1
  85. package/lib/util/deposit.d.ts +2 -2
  86. package/lib/util/deposit.d.ts.map +1 -1
  87. package/lib/util/deposit.js.map +1 -1
  88. package/lib/util/genesis.d.ts.map +1 -1
  89. package/lib/util/genesis.js +46 -23
  90. package/lib/util/genesis.js.map +1 -1
  91. package/lib/util/inclusionListCommittee.d.ts +9 -0
  92. package/lib/util/inclusionListCommittee.d.ts.map +1 -0
  93. package/lib/util/inclusionListCommittee.js +18 -0
  94. package/lib/util/inclusionListCommittee.js.map +1 -0
  95. package/lib/util/index.d.ts +1 -0
  96. package/lib/util/index.d.ts.map +1 -1
  97. package/lib/util/index.js +1 -0
  98. package/lib/util/index.js.map +1 -1
  99. package/lib/util/shuffling.d.ts +3 -0
  100. package/lib/util/shuffling.d.ts.map +1 -1
  101. package/lib/util/shuffling.js +41 -1
  102. package/lib/util/shuffling.js.map +1 -1
  103. package/lib/util/slot.d.ts +18 -2
  104. package/lib/util/slot.d.ts.map +1 -1
  105. package/lib/util/slot.js +70 -6
  106. package/lib/util/slot.js.map +1 -1
  107. package/lib/util/syncCommittee.d.ts +4 -3
  108. package/lib/util/syncCommittee.d.ts.map +1 -1
  109. package/lib/util/syncCommittee.js +9 -7
  110. package/lib/util/syncCommittee.js.map +1 -1
  111. package/lib/util/validator.d.ts +1 -1
  112. package/lib/util/validator.d.ts.map +1 -1
  113. package/lib/util/validator.js +32 -11
  114. package/lib/util/validator.js.map +1 -1
  115. package/lib/util/weakSubjectivity.d.ts.map +1 -1
  116. package/lib/util/weakSubjectivity.js +4 -3
  117. package/lib/util/weakSubjectivity.js.map +1 -1
  118. package/package.json +8 -8
  119. package/src/block/index.ts +4 -3
  120. package/src/block/processAttestationsAltair.ts +4 -1
  121. package/src/block/processDeposit.ts +7 -2
  122. package/src/block/processEth1Data.ts +3 -3
  123. package/src/block/processOperations.ts +14 -11
  124. package/src/cache/epochCache.ts +28 -4
  125. package/src/cache/epochTransitionCache.ts +2 -1
  126. package/src/cache/stateCache.ts +2 -0
  127. package/src/cache/types.ts +2 -1
  128. package/src/epoch/getRewardsAndPenalties.ts +11 -1
  129. package/src/epoch/index.ts +4 -1
  130. package/src/epoch/processEth1DataReset.ts +2 -2
  131. package/src/index.ts +2 -0
  132. package/src/metrics.ts +5 -4
  133. package/src/rewards/attestationsRewards.ts +12 -1
  134. package/src/signatureSets/inclusionList.ts +20 -0
  135. package/src/signatureSets/index.ts +1 -0
  136. package/src/slot/upgradeStateToHeze.ts +6 -4
  137. package/src/stateTransition.ts +2 -2
  138. package/src/stateView/beaconStateView.ts +9 -1
  139. package/src/stateView/interface.ts +4 -0
  140. package/src/stateView/nativeBeaconStateView.ts +14 -0
  141. package/src/types.ts +2 -0
  142. package/src/util/altair.ts +15 -4
  143. package/src/util/blindedBlock.ts +8 -3
  144. package/src/util/clock.ts +26 -17
  145. package/src/util/deposit.ts +2 -2
  146. package/src/util/genesis.ts +56 -30
  147. package/src/util/inclusionListCommittee.ts +17 -0
  148. package/src/util/index.ts +1 -0
  149. package/src/util/shuffling.ts +46 -1
  150. package/src/util/slot.ts +78 -8
  151. package/src/util/syncCommittee.ts +21 -9
  152. package/src/util/validator.ts +47 -20
  153. package/src/util/weakSubjectivity.ts +3 -3
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  EFFECTIVE_BALANCE_INCREMENT,
3
3
  ForkSeq,
4
+ GENESIS_EPOCH,
4
5
  INACTIVITY_PENALTY_QUOTIENT_ALTAIR,
5
6
  INACTIVITY_PENALTY_QUOTIENT_BELLATRIX,
6
7
  PARTICIPATION_FLAG_WEIGHTS,
@@ -18,6 +19,7 @@ import {
18
19
  hasMarkers,
19
20
  } from "../util/attesterStatus.js";
20
21
  import {isInInactivityLeak} from "../util/index.js";
22
+ import {getSlotDurationMsAtEpoch} from "../util/slot.js";
21
23
 
22
24
  type RewardPenaltyItem = {
23
25
  baseReward: number;
@@ -67,7 +69,15 @@ export function getRewardsAndPenaltiesAltair(
67
69
 
68
70
  const inactivityPenalityMultiplier =
69
71
  fork === ForkSeq.altair ? INACTIVITY_PENALTY_QUOTIENT_ALTAIR : INACTIVITY_PENALTY_QUOTIENT_BELLATRIX;
70
- const penaltyDenominator = config.INACTIVITY_SCORE_BIAS * inactivityPenalityMultiplier;
72
+ let penaltyDenominator = config.INACTIVITY_SCORE_BIAS * inactivityPenalityMultiplier;
73
+ // The penalty pays for the previous epoch and scales with the square of its slot duration
74
+ const genesisSlotDurationMs = getSlotDurationMsAtEpoch(config, GENESIS_EPOCH);
75
+ const prevEpochSlotDurationMs = getSlotDurationMsAtEpoch(config, Math.max(epochCtx.epoch - 1, GENESIS_EPOCH));
76
+ if (prevEpochSlotDurationMs !== genesisSlotDurationMs) {
77
+ penaltyDenominator = Number(
78
+ (BigInt(penaltyDenominator) * BigInt(genesisSlotDurationMs) ** 2n) / BigInt(prevEpochSlotDurationMs) ** 2n
79
+ );
80
+ }
71
81
 
72
82
  const {flags} = cache;
73
83
  for (let i = 0; i < flags.length; i++) {
@@ -14,6 +14,7 @@ import {
14
14
  CachedBeaconStateFulu,
15
15
  CachedBeaconStateGloas,
16
16
  CachedBeaconStatePhase0,
17
+ CachedBeaconStatePreHeze,
17
18
  EpochTransitionCache,
18
19
  } from "../types.js";
19
20
  import {processBuilderPendingPayments} from "./processBuilderPendingPayments.js";
@@ -140,7 +141,9 @@ export function processEpoch(
140
141
  timer?.();
141
142
  }
142
143
 
143
- processEth1DataReset(state, cache);
144
+ if (fork < ForkSeq.heze) {
145
+ processEth1DataReset(state as CachedBeaconStatePreHeze, cache);
146
+ }
144
147
 
145
148
  if (fork >= ForkSeq.electra) {
146
149
  const stateElectra = state as CachedBeaconStateElectra;
@@ -1,13 +1,13 @@
1
1
  import {EPOCHS_PER_ETH1_VOTING_PERIOD} from "@lodestar/params";
2
2
  import {ssz} from "@lodestar/types";
3
- import {CachedBeaconStateAllForks, EpochTransitionCache} from "../types.js";
3
+ import {CachedBeaconStatePreHeze, EpochTransitionCache} from "../types.js";
4
4
 
5
5
  /**
6
6
  * Reset eth1DataVotes tree every `EPOCHS_PER_ETH1_VOTING_PERIOD`.
7
7
  *
8
8
  * PERF: Almost no (constant) cost
9
9
  */
10
- export function processEth1DataReset(state: CachedBeaconStateAllForks, cache: EpochTransitionCache): void {
10
+ export function processEth1DataReset(state: CachedBeaconStatePreHeze, cache: EpochTransitionCache): void {
11
11
  const nextEpoch = cache.currentEpoch + 1;
12
12
 
13
13
  // reset eth1 data votes
package/src/index.ts CHANGED
@@ -80,6 +80,7 @@ export type {
80
80
  BeaconStateHeze,
81
81
  // Non-cached states
82
82
  BeaconStatePhase0,
83
+ BeaconStatePreHeze,
83
84
  CachedBeaconStateAllForks,
84
85
  CachedBeaconStateAltair,
85
86
  CachedBeaconStateBellatrix,
@@ -91,5 +92,6 @@ export type {
91
92
  CachedBeaconStateGloas,
92
93
  CachedBeaconStateHeze,
93
94
  CachedBeaconStatePhase0,
95
+ CachedBeaconStatePreHeze,
94
96
  } from "./types.js";
95
97
  export * from "./util/index.js";
package/src/metrics.ts CHANGED
@@ -156,19 +156,20 @@ export function getMetrics(register: MetricsRegister) {
156
156
  }
157
157
 
158
158
  export function onStateCloneMetrics(
159
- state: CachedBeaconStateAllForks,
159
+ clonedState: CachedBeaconStateAllForks,
160
+ clonedCount: number,
160
161
  metrics: BeaconStateTransitionMetrics,
161
162
  source: StateCloneSource
162
163
  ): void {
163
- metrics.preStateClonedCount.observe(state.clonedCount);
164
+ metrics.preStateClonedCount.observe(clonedCount);
164
165
 
165
- if (isBalancesNodesPopulated(state)) {
166
+ if (isBalancesNodesPopulated(clonedState)) {
166
167
  metrics.preStateBalancesNodesPopulatedHit.inc({source});
167
168
  } else {
168
169
  metrics.preStateBalancesNodesPopulatedMiss.inc({source});
169
170
  }
170
171
 
171
- if (isValidatorsNodesPopulated(state)) {
172
+ if (isValidatorsNodesPopulated(clonedState)) {
172
173
  metrics.preStateValidatorsNodesPopulatedHit.inc({source});
173
174
  } else {
174
175
  metrics.preStateValidatorsNodesPopulatedMiss.inc({source});
@@ -4,6 +4,7 @@ import {
4
4
  EFFECTIVE_BALANCE_INCREMENT,
5
5
  ForkName,
6
6
  ForkSeq,
7
+ GENESIS_EPOCH,
7
8
  INACTIVITY_PENALTY_QUOTIENT_ALTAIR,
8
9
  INACTIVITY_PENALTY_QUOTIENT_BELLATRIX,
9
10
  MAX_EFFECTIVE_BALANCE,
@@ -26,6 +27,7 @@ import {
26
27
  FLAG_PREV_HEAD_ATTESTER_UNSLASHED,
27
28
  FLAG_PREV_SOURCE_ATTESTER_UNSLASHED,
28
29
  FLAG_PREV_TARGET_ATTESTER_UNSLASHED,
30
+ getSlotDurationMsAtEpoch,
29
31
  hasMarkers,
30
32
  isInInactivityLeak,
31
33
  } from "../util/index.js";
@@ -168,7 +170,16 @@ function computeTotalAttestationsRewardsAltair(
168
170
  config.getForkSeq(state.slot) === ForkSeq.altair
169
171
  ? INACTIVITY_PENALTY_QUOTIENT_ALTAIR
170
172
  : INACTIVITY_PENALTY_QUOTIENT_BELLATRIX;
171
- const inactivityPenaltyDenominator = config.INACTIVITY_SCORE_BIAS * inactivityPenaltyQuotient;
173
+ let inactivityPenaltyDenominator = config.INACTIVITY_SCORE_BIAS * inactivityPenaltyQuotient;
174
+ // The penalty pays for the previous epoch and scales with the square of its slot duration
175
+ const genesisSlotDurationMs = getSlotDurationMsAtEpoch(config, GENESIS_EPOCH);
176
+ const prevEpochSlotDurationMs = getSlotDurationMsAtEpoch(config, Math.max(epochCtx.epoch - 1, GENESIS_EPOCH));
177
+ if (prevEpochSlotDurationMs !== genesisSlotDurationMs) {
178
+ inactivityPenaltyDenominator = Number(
179
+ (BigInt(inactivityPenaltyDenominator) * BigInt(genesisSlotDurationMs) ** 2n) /
180
+ BigInt(prevEpochSlotDurationMs) ** 2n
181
+ );
182
+ }
172
183
 
173
184
  for (let i = 0; i < flags.length; i++) {
174
185
  if (validatorIndices.length && !validatorIndices.includes(i)) {
@@ -0,0 +1,20 @@
1
+ import {BeaconConfig} from "@lodestar/config";
2
+ import {DOMAIN_INCLUSION_LIST_COMMITTEE} from "@lodestar/params";
3
+ import {Slot, heze, ssz} from "@lodestar/types";
4
+ import {ISignatureSet, SignatureSetType, computeSigningRoot} from "../util/index.js";
5
+
6
+ export function getInclusionListSignatureSet(
7
+ config: BeaconConfig,
8
+ stateSlot: Slot,
9
+ signedInclusionList: heze.SignedInclusionList
10
+ ): ISignatureSet {
11
+ const message = signedInclusionList.message;
12
+ const domain = config.getDomain(stateSlot, DOMAIN_INCLUSION_LIST_COMMITTEE, message.slot);
13
+
14
+ return {
15
+ type: SignatureSetType.indexed,
16
+ index: message.validatorIndex,
17
+ signingRoot: computeSigningRoot(ssz.heze.InclusionList, message, domain),
18
+ signature: signedInclusionList.signature,
19
+ };
20
+ }
@@ -18,6 +18,7 @@ export * from "./attesterSlashings.js";
18
18
  export * from "./blsToExecutionChange.js";
19
19
  export * from "./executionPayloadBid.js";
20
20
  export * from "./executionPayloadEnvelope.js";
21
+ export * from "./inclusionList.js";
21
22
  export * from "./indexedAttestation.js";
22
23
  export * from "./indexedPayloadAttestation.js";
23
24
  export * from "./proposer.js";
@@ -7,6 +7,12 @@ import {CachedBeaconStateGloas, CachedBeaconStateHeze} from "../types.js";
7
7
  * Spec: heze/fork.md `upgrade_to_heze`.
8
8
  */
9
9
  export function upgradeStateToHeze(stateGloas: CachedBeaconStateGloas): CachedBeaconStateHeze {
10
+ // In Heze, the old deposit mechanism must be fully drained before the fork
11
+ if (BigInt(stateGloas.eth1DepositIndex) !== stateGloas.depositRequestsStartIndex) {
12
+ throw new Error(
13
+ `Cannot upgrade to Heze: legacy deposit mechanism still active eth1DepositIndex=${stateGloas.eth1DepositIndex} depositRequestsStartIndex=${stateGloas.depositRequestsStartIndex}`
14
+ );
15
+ }
10
16
  const {config} = stateGloas;
11
17
 
12
18
  ssz.gloas.BeaconState.commitViewDU(stateGloas);
@@ -26,9 +32,6 @@ export function upgradeStateToHeze(stateGloas: CachedBeaconStateGloas): CachedBe
26
32
  stateHezeView.blockRoots = stateHezeCloned.blockRoots;
27
33
  stateHezeView.stateRoots = stateHezeCloned.stateRoots;
28
34
  stateHezeView.historicalRoots = stateHezeCloned.historicalRoots;
29
- stateHezeView.eth1Data = stateHezeCloned.eth1Data;
30
- stateHezeView.eth1DataVotes = stateHezeCloned.eth1DataVotes;
31
- stateHezeView.eth1DepositIndex = stateHezeCloned.eth1DepositIndex;
32
35
  stateHezeView.validators = stateHezeCloned.validators;
33
36
  stateHezeView.balances = stateHezeCloned.balances;
34
37
  stateHezeView.randaoMixes = stateHezeCloned.randaoMixes;
@@ -46,7 +49,6 @@ export function upgradeStateToHeze(stateGloas: CachedBeaconStateGloas): CachedBe
46
49
  stateHezeView.nextWithdrawalIndex = stateHezeCloned.nextWithdrawalIndex;
47
50
  stateHezeView.nextWithdrawalValidatorIndex = stateHezeCloned.nextWithdrawalValidatorIndex;
48
51
  stateHezeView.historicalSummaries = stateHezeCloned.historicalSummaries;
49
- stateHezeView.depositRequestsStartIndex = stateHezeCloned.depositRequestsStartIndex;
50
52
  stateHezeView.depositBalanceToConsume = stateHezeCloned.depositBalanceToConsume;
51
53
  stateHezeView.exitBalanceToConsume = stateHezeCloned.exitBalanceToConsume;
52
54
  stateHezeView.earliestExitEpoch = stateHezeCloned.earliestExitEpoch;
@@ -101,7 +101,7 @@ export function stateTransition(
101
101
  let postState = state.clone(options.dontTransferCache);
102
102
 
103
103
  if (metrics) {
104
- onStateCloneMetrics(postState, metrics, StateCloneSource.stateTransition);
104
+ onStateCloneMetrics(postState, state.clonedCount, metrics, StateCloneSource.stateTransition);
105
105
  }
106
106
 
107
107
  // State is already a ViewDU, which won't commit changes. Equivalent to .setStateCachesAsTransient()
@@ -168,7 +168,7 @@ export function processSlots(
168
168
  let postState = state.clone(opts?.dontTransferCache);
169
169
 
170
170
  if (metrics) {
171
- onStateCloneMetrics(postState, metrics, StateCloneSource.processSlots);
171
+ onStateCloneMetrics(postState, state.clonedCount, metrics, StateCloneSource.processSlots);
172
172
  }
173
173
 
174
174
  // State is already a ViewDU, which won't commit changes. Equivalent to .setStateCachesAsTransient()
@@ -40,6 +40,7 @@ import {
40
40
  CachedBeaconStateExecutions,
41
41
  CachedBeaconStateFulu,
42
42
  CachedBeaconStateGloas,
43
+ CachedBeaconStatePreHeze,
43
44
  createCachedBeaconState,
44
45
  isStateValidatorsNodesPopulated,
45
46
  } from "../cache/stateCache.js";
@@ -145,7 +146,10 @@ export class BeaconStateView implements IBeaconStateViewLatestFork {
145
146
  }
146
147
 
147
148
  get eth1Data(): phase0.Eth1Data {
148
- return this.cachedState.eth1Data;
149
+ if (this.forkSeq >= ForkSeq.heze) {
150
+ throw new Error("eth1Data was removed from the beacon state in Heze");
151
+ }
152
+ return (this.cachedState as CachedBeaconStatePreHeze).eth1Data;
149
153
  }
150
154
 
151
155
  get latestBlockHeader(): phase0.BeaconBlockHeader {
@@ -611,6 +615,10 @@ export class BeaconStateView implements IBeaconStateViewLatestFork {
611
615
  return this.cachedState.epochCtx.currentSyncCommitteeIndexed;
612
616
  }
613
617
 
618
+ get currentSyncCommitteeValidatorIndices(): Uint32Array {
619
+ return this.cachedState.epochCtx.currentSyncCommitteeIndexed.validatorIndices;
620
+ }
621
+
614
622
  get syncProposerReward(): number {
615
623
  return this.cachedState.epochCtx.syncProposerReward;
616
624
  }
@@ -207,6 +207,7 @@ export interface IBeaconStateViewAltair extends IBeaconStateView {
207
207
  currentSyncCommittee: altair.SyncCommittee;
208
208
  nextSyncCommittee: altair.SyncCommittee;
209
209
  currentSyncCommitteeIndexed: SyncCommitteeCache;
210
+ readonly currentSyncCommitteeValidatorIndices: Uint32Array;
210
211
  syncProposerReward: number;
211
212
  getIndexedSyncCommitteeAtEpoch(epoch: Epoch): SyncCommitteeCache;
212
213
  /** Get indexed sync committee with slot+1 offset for duty lookups */
@@ -341,6 +342,7 @@ export type IBeaconStateViewNative = Omit<
341
342
  | "computeBlockRewards"
342
343
  | "computeNewStateRoot"
343
344
  | "computeSyncCommitteeRewards"
345
+ | "currentSyncCommitteeValidatorIndices"
344
346
  | "eth1Data"
345
347
  | "executionPayloadAvailability"
346
348
  | "getBeaconCommittee"
@@ -358,6 +360,8 @@ export type IBeaconStateViewNative = Omit<
358
360
  | "withParentPayloadApplied"
359
361
  > & {
360
362
  pendingDeposits: Uint8Array;
363
+ /** Older native addons expose indices only through currentSyncCommitteeIndexed. */
364
+ readonly currentSyncCommitteeValidatorIndices?: Uint32Array;
361
365
  pendingPartialWithdrawals: Uint8Array;
362
366
  pendingConsolidations: Uint8Array;
363
367
  proposerLookahead: Uint32Array;
@@ -97,6 +97,7 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
97
97
  private cachedPreviousEpochParticipation: Uint8Array | null = null;
98
98
  private cachedCurrentEpochParticipation: Uint8Array | null = null;
99
99
  private cachedCurrentSyncCommitteeIndexed: SyncCommitteeCache | null = null;
100
+ private cachedCurrentSyncCommitteeValidatorIndices: Uint32Array | null = null;
100
101
  private cachedSyncProposerReward: number | null = null;
101
102
  // bellatrix
102
103
  private cachedLatestExecutionPayloadHeader: ExecutionPayloadHeader | null = null;
@@ -228,6 +229,9 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
228
229
  }
229
230
 
230
231
  get eth1Data(): phase0.Eth1Data {
232
+ if (this.forkSeq >= ForkSeq.heze) {
233
+ throw new Error("eth1Data was removed from the beacon state in Heze");
234
+ }
231
235
  if (this.cachedEth1Data === null) {
232
236
  this.cachedEth1Data = this.binding.eth1Data;
233
237
  }
@@ -719,6 +723,16 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
719
723
  return this.cachedCurrentSyncCommitteeIndexed;
720
724
  }
721
725
 
726
+ get currentSyncCommitteeValidatorIndices(): Uint32Array {
727
+ if (this.cachedCurrentSyncCommitteeValidatorIndices === null) {
728
+ this.cachedCurrentSyncCommitteeValidatorIndices =
729
+ this.cachedCurrentSyncCommitteeIndexed?.validatorIndices ??
730
+ this.binding.currentSyncCommitteeValidatorIndices ??
731
+ this.currentSyncCommitteeIndexed.validatorIndices;
732
+ }
733
+ return this.cachedCurrentSyncCommitteeValidatorIndices;
734
+ }
735
+
722
736
  get syncProposerReward(): number {
723
737
  if (this.cachedSyncProposerReward === null) {
724
738
  this.cachedSyncProposerReward = this.binding.syncProposerReward;
package/src/types.ts CHANGED
@@ -12,6 +12,7 @@ export type {
12
12
  CachedBeaconStateGloas,
13
13
  CachedBeaconStateHeze,
14
14
  CachedBeaconStatePhase0,
15
+ CachedBeaconStatePreHeze,
15
16
  } from "./cache/stateCache.js";
16
17
  export type {
17
18
  BeaconStateAllForks,
@@ -25,5 +26,6 @@ export type {
25
26
  BeaconStateGloas,
26
27
  BeaconStateHeze,
27
28
  BeaconStatePhase0,
29
+ BeaconStatePreHeze,
28
30
  ShufflingGetter,
29
31
  } from "./cache/types.js";
@@ -1,13 +1,24 @@
1
- import {BASE_REWARD_FACTOR, EFFECTIVE_BALANCE_INCREMENT} from "@lodestar/params";
1
+ import {ChainConfig} from "@lodestar/config";
2
+ import {BASE_REWARD_FACTOR, EFFECTIVE_BALANCE_INCREMENT, GENESIS_EPOCH} from "@lodestar/params";
3
+ import {Epoch} from "@lodestar/types";
2
4
  import {bigIntSqrt, bnToNum} from "@lodestar/utils";
5
+ import {getSlotDurationMsAtEpoch} from "./slot.js";
3
6
 
4
7
  /**
5
8
  * Before we manage bigIntSqrt(totalActiveStake) as BigInt and return BigInt.
6
9
  * bigIntSqrt(totalActiveStake) should fit a number (2 ** 53 -1 max)
7
10
  **/
8
- export function computeBaseRewardPerIncrement(totalActiveStakeByIncrement: number): number {
11
+ export function computeBaseRewardPerIncrement(
12
+ config: ChainConfig,
13
+ epoch: Epoch,
14
+ totalActiveStakeByIncrement: number
15
+ ): number {
16
+ // Divide last so the exact slot duration ratio applies
17
+ const numerator = Math.floor(
18
+ (EFFECTIVE_BALANCE_INCREMENT * BASE_REWARD_FACTOR * getSlotDurationMsAtEpoch(config, epoch)) /
19
+ getSlotDurationMsAtEpoch(config, GENESIS_EPOCH)
20
+ );
9
21
  return Math.floor(
10
- (EFFECTIVE_BALANCE_INCREMENT * BASE_REWARD_FACTOR) /
11
- bnToNum(bigIntSqrt(BigInt(totalActiveStakeByIncrement) * BigInt(EFFECTIVE_BALANCE_INCREMENT)))
22
+ numerator / bnToNum(bigIntSqrt(BigInt(totalActiveStakeByIncrement) * BigInt(EFFECTIVE_BALANCE_INCREMENT)))
12
23
  );
13
24
  }
@@ -3,10 +3,12 @@ import {
3
3
  ForkName,
4
4
  ForkPostBellatrix,
5
5
  ForkPostDeneb,
6
+ ForkPreDeneb,
6
7
  ForkPreGloas,
7
8
  ForkSeq,
8
9
  isForkPostBellatrix,
9
10
  isForkPostDeneb,
11
+ isForkPostGloas,
10
12
  } from "@lodestar/params";
11
13
  import {
12
14
  BeaconBlock,
@@ -128,7 +130,10 @@ export function reconstructSignedBlockContents(
128
130
  signedBlindedBlock: SignedBlindedBeaconBlock,
129
131
  executionPayload: ExecutionPayload | null,
130
132
  blobsBundle: BlobsBundle | null
131
- ): SignedBlockContents {
133
+ ): SignedBlockContents<ForkPreGloas> {
134
+ if (isForkPostGloas(fork)) {
135
+ throw Error("Blinded block reconstruction is not supported after Gloas");
136
+ }
132
137
  const signedBlock = signedBlindedBlockToFull(fork, signedBlindedBlock, executionPayload);
133
138
 
134
139
  if (isForkPostDeneb(fork)) {
@@ -136,10 +141,10 @@ export function reconstructSignedBlockContents(
136
141
  throw Error("Missing blobs bundle to reconstruct post-deneb block contents");
137
142
  }
138
143
  return {
139
- signedBlock: signedBlock as SignedBeaconBlock<ForkPostDeneb>,
144
+ signedBlock: signedBlock as SignedBeaconBlock<ForkPostDeneb & ForkPreGloas>,
140
145
  kzgProofs: blobsBundle.proofs,
141
146
  blobs: blobsBundle.blobs,
142
147
  };
143
148
  }
144
- return {signedBlock};
149
+ return {signedBlock: signedBlock as SignedBeaconBlock<ForkPreDeneb>};
145
150
  }
package/src/util/clock.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import {ChainForkConfig} from "@lodestar/config";
2
- import {GENESIS_SLOT, SLOTS_PER_EPOCH} from "@lodestar/params";
2
+ import {SLOTS_PER_EPOCH} from "@lodestar/params";
3
3
  import {Epoch, Slot, TimeSeconds} from "@lodestar/types";
4
4
  import {ErrorAborted, Logger, isErrorAborted, sleep} from "@lodestar/utils";
5
5
  import {computeEpochAtSlot} from "./epoch.js";
6
- import {computeTimeAtSlot, getCurrentSlot} from "./slot.js";
6
+ import {computeTimeAtSlot, getCurrentSlot, getSlotDurationMs} from "./slot.js";
7
7
 
8
8
  type RunEveryFn = (slot: Slot, signal: AbortSignal) => Promise<void>;
9
9
 
@@ -13,6 +13,7 @@ export type ClockOptions = {
13
13
 
14
14
  export interface IClock {
15
15
  readonly genesisTime: number;
16
+ /** @deprecated Use getSlotDurationMs(slot) instead */
16
17
  readonly secondsPerSlot: number;
17
18
 
18
19
  readonly currentEpoch: number;
@@ -25,6 +26,8 @@ export interface IClock {
25
26
  secFromSlot(slot: Slot): number;
26
27
  getCurrentSlot(): Slot;
27
28
  getCurrentEpoch(): Epoch;
29
+ /** Slot duration in ms for a given slot */
30
+ getSlotDurationMs(slot: Slot): number;
28
31
  }
29
32
 
30
33
  export enum TimeItem {
@@ -95,6 +98,11 @@ export class Clock implements IClock {
95
98
  return Date.now() / 1000 - computeTimeAtSlot(this.config, slot, this.genesisTime);
96
99
  }
97
100
 
101
+ /** Slot duration in ms for a given slot */
102
+ getSlotDurationMs(slot: Slot): number {
103
+ return getSlotDurationMs(this.config, slot);
104
+ }
105
+
98
106
  /**
99
107
  * If a task happens to take more than one slot to run, we might skip a slot. This is unfortunate,
100
108
  * however the alternative is to *always* process every slot, which has the chance of creating a
@@ -125,7 +133,7 @@ export class Clock implements IClock {
125
133
  if (timeItem === TimeItem.Slot) {
126
134
  if (nextSlot > slot + 1) {
127
135
  // It's not very likely that we skip more than one slot as HTTP timeout is set
128
- // to SLOT_DURATION_MS so we will fail task before skipping another slot.
136
+ // to at most one slot duration so we will fail task before skipping another slot.
129
137
  this.logger.warn("Skipped slot due to task taking more than one slot to run", {
130
138
  skippedSlot: slot + 1,
131
139
  });
@@ -145,20 +153,17 @@ export class Clock implements IClock {
145
153
  }
146
154
 
147
155
  private timeUntilNext(timeItem: TimeItem): number {
148
- const milliSecondsPerSlot = this.config.SLOT_DURATION_MS;
149
- const msFromGenesis = Date.now() - this.genesisTime * 1000;
156
+ const currentSlot = getCurrentSlot(this.config, this.genesisTime);
150
157
 
151
158
  if (timeItem === TimeItem.Slot) {
152
- if (msFromGenesis >= 0) {
153
- return milliSecondsPerSlot - (msFromGenesis % milliSecondsPerSlot);
154
- }
155
- return Math.abs(msFromGenesis) % milliSecondsPerSlot;
159
+ const nextSlotTimeSec = computeTimeAtSlot(this.config, currentSlot + 1, this.genesisTime);
160
+ return Math.max(0, nextSlotTimeSec * 1000 - Date.now());
156
161
  }
157
- const milliSecondsPerEpoch = SLOTS_PER_EPOCH * milliSecondsPerSlot;
158
- if (msFromGenesis >= 0) {
159
- return milliSecondsPerEpoch - (msFromGenesis % milliSecondsPerEpoch);
160
- }
161
- return Math.abs(msFromGenesis) % milliSecondsPerEpoch;
162
+
163
+ const currentEpoch = computeEpochAtSlot(currentSlot);
164
+ const nextEpochSlot = (currentEpoch + 1) * SLOTS_PER_EPOCH;
165
+ const nextEpochTimeSec = computeTimeAtSlot(this.config, nextEpochSlot, this.genesisTime);
166
+ return Math.max(0, nextEpochTimeSec * 1000 - Date.now());
162
167
  }
163
168
  }
164
169
 
@@ -166,9 +171,13 @@ export class Clock implements IClock {
166
171
  * Same to the spec but we use Math.round instead of Math.floor.
167
172
  */
168
173
  export function getCurrentSlotAround(config: ChainForkConfig, genesisTime: TimeSeconds): Slot {
169
- const diffInSeconds = Date.now() / 1000 - genesisTime;
170
- const slotsSinceGenesis = Math.round((diffInSeconds * 1000) / config.SLOT_DURATION_MS);
171
- return GENESIS_SLOT + slotsSinceGenesis;
174
+ const floorSlot = getCurrentSlot(config, genesisTime);
175
+ const slotStartSec = computeTimeAtSlot(config, floorSlot, genesisTime);
176
+ const slotDurationMs = getSlotDurationMs(config, floorSlot);
177
+ const slotDurationSec = slotDurationMs / 1000;
178
+ const elapsed = Date.now() / 1000 - slotStartSec;
179
+
180
+ return elapsed >= slotDurationSec / 2 ? floorSlot + 1 : floorSlot;
172
181
  }
173
182
 
174
183
  // function useEventStream() {
@@ -1,8 +1,8 @@
1
1
  import {ForkSeq, MAX_DEPOSITS} from "@lodestar/params";
2
2
  import {UintNum64, phase0} from "@lodestar/types";
3
- import {CachedBeaconStateAllForks, CachedBeaconStateElectra} from "../types.js";
3
+ import {CachedBeaconStateElectra, CachedBeaconStatePreHeze} from "../types.js";
4
4
 
5
- export function getEth1DepositCount(state: CachedBeaconStateAllForks, eth1Data?: phase0.Eth1Data): UintNum64 {
5
+ export function getEth1DepositCount(state: CachedBeaconStatePreHeze, eth1Data?: phase0.Eth1Data): UintNum64 {
6
6
  const eth1DataToUse = eth1Data ?? state.eth1Data;
7
7
  // Proposer can set any value, use in bigint until the result is bounded by MAX_DEPOSITS
8
8
  const depositCount = eth1DataToUse.depositCount;
@@ -11,11 +11,18 @@ import {
11
11
  UNSET_DEPOSIT_REQUESTS_START_INDEX,
12
12
  } from "@lodestar/params";
13
13
  import {Bytes32, Root, TimeSeconds, phase0, ssz} from "@lodestar/types";
14
- import {processDeposit} from "../block/processDeposit.js";
14
+ import {applyDeposit, processDeposit} from "../block/processDeposit.js";
15
15
  import {EpochCacheImmutableData} from "../cache/epochCache.js";
16
16
  import {createCachedBeaconState} from "../cache/stateCache.js";
17
17
  import {increaseBalance} from "../index.js";
18
- import {BeaconStateAllForks, CachedBeaconStateAllForks, CachedBeaconStateElectra} from "../types.js";
18
+ import {
19
+ BeaconStateAllForks,
20
+ BeaconStatePreHeze,
21
+ CachedBeaconStateAllForks,
22
+ CachedBeaconStateElectra,
23
+ CachedBeaconStateGloas,
24
+ CachedBeaconStatePreHeze,
25
+ } from "../types.js";
19
26
  import {newFilledArray} from "./array.js";
20
27
  import {getTemporaryBlockHeader} from "./blockRoot.js";
21
28
  import {computeEpochAtSlot} from "./epoch.js";
@@ -87,7 +94,9 @@ export function getGenesisBeaconState(
87
94
  state.latestBlockHeader = ssz.phase0.BeaconBlockHeader.toViewDU(latestBlockHeader);
88
95
 
89
96
  // Ethereum 1.0 chain data
90
- state.eth1Data = ssz.phase0.Eth1Data.toViewDU(genesisEth1Data);
97
+ if (config.getForkSeq(GENESIS_SLOT) < ForkSeq.heze) {
98
+ (state as BeaconStatePreHeze).eth1Data = ssz.phase0.Eth1Data.toViewDU(genesisEth1Data);
99
+ }
91
100
  state.randaoMixes = ssz.phase0.RandaoMixes.toViewDU(randaoMixes);
92
101
 
93
102
  return state;
@@ -100,7 +109,9 @@ export function getGenesisBeaconState(
100
109
  * @param eth1BlockHash eth1 block hash
101
110
  */
102
111
  export function applyEth1BlockHash(state: CachedBeaconStateAllForks, eth1BlockHash: Bytes32): void {
103
- state.eth1Data.blockHash = eth1BlockHash;
112
+ if (state.config.getForkSeq(state.slot) < ForkSeq.heze) {
113
+ (state as CachedBeaconStatePreHeze).eth1Data.blockHash = eth1BlockHash;
114
+ }
104
115
  state.randaoMixes = ssz.phase0.RandaoMixes.toViewDU(newFilledArray(EPOCHS_PER_HISTORICAL_VECTOR, eth1BlockHash));
105
116
  }
106
117
 
@@ -134,36 +145,41 @@ export function applyDeposits(
134
145
  fullDepositDataRootList?: DepositDataRootViewDU
135
146
  ): {activatedValidatorCount: number} {
136
147
  const fork = config.getForkSeq(state.slot);
137
- const depositDataRootList: Root[] = [];
138
-
139
- const fullDepositDataRootArr = fullDepositDataRootList ? fullDepositDataRootList.getAllReadonlyValues() : null;
140
-
141
- if (fullDepositDataRootArr) {
142
- const depositCount = Number(state.eth1Data.depositCount);
143
- for (let index = 0; index < depositCount; index++) {
144
- depositDataRootList.push(fullDepositDataRootArr[index]);
148
+ if (fork >= ForkSeq.heze) {
149
+ // Genesis deposits do not use the removed legacy deposit tree.
150
+ for (const deposit of newDeposits) {
151
+ applyDeposit(fork, state, deposit.data);
145
152
  }
146
- }
153
+ } else {
154
+ const statePreHeze = state as CachedBeaconStatePreHeze;
155
+ const depositDataRootList: Root[] = [];
156
+ const fullDepositDataRootArr = fullDepositDataRootList ? fullDepositDataRootList.getAllReadonlyValues() : null;
147
157
 
148
- const initDepositCount = depositDataRootList.length;
149
- const depositDatas = fullDepositDataRootList ? null : newDeposits.map((deposit) => deposit.data);
150
- const {DepositData, DepositDataRootList} = ssz.phase0;
151
-
152
- for (const [index, deposit] of newDeposits.entries()) {
153
158
  if (fullDepositDataRootArr) {
154
- depositDataRootList.push(fullDepositDataRootArr[index + initDepositCount]);
155
- state.eth1Data.depositRoot = DepositDataRootList.hashTreeRoot(depositDataRootList);
156
- } else if (depositDatas) {
157
- const depositDataList = depositDatas.slice(0, index + 1);
158
- state.eth1Data.depositRoot = DepositDataRootList.hashTreeRoot(
159
- depositDataList.map((d) => DepositData.hashTreeRoot(d))
160
- );
159
+ const depositCount = Number(statePreHeze.eth1Data.depositCount);
160
+ for (let index = 0; index < depositCount; index++) {
161
+ depositDataRootList.push(fullDepositDataRootArr[index]);
162
+ }
161
163
  }
162
164
 
163
- state.eth1Data.depositCount += 1n;
165
+ const initDepositCount = depositDataRootList.length;
166
+ const depositDatas = fullDepositDataRootList ? null : newDeposits.map((deposit) => deposit.data);
167
+ const {DepositData, DepositDataRootList} = ssz.phase0;
168
+
169
+ for (const [index, deposit] of newDeposits.entries()) {
170
+ if (fullDepositDataRootArr) {
171
+ depositDataRootList.push(fullDepositDataRootArr[index + initDepositCount]);
172
+ statePreHeze.eth1Data.depositRoot = DepositDataRootList.hashTreeRoot(depositDataRootList);
173
+ } else if (depositDatas) {
174
+ const depositDataList = depositDatas.slice(0, index + 1);
175
+ statePreHeze.eth1Data.depositRoot = DepositDataRootList.hashTreeRoot(
176
+ depositDataList.map((d) => DepositData.hashTreeRoot(d))
177
+ );
178
+ }
164
179
 
165
- const fork = config.getForkSeq(GENESIS_SLOT);
166
- processDeposit(fork, state, deposit);
180
+ statePreHeze.eth1Data.depositCount += 1n;
181
+ processDeposit(fork, statePreHeze, deposit);
182
+ }
167
183
  }
168
184
 
169
185
  // Process deposit balance updates
@@ -178,7 +194,11 @@ export function applyDeposits(
178
194
  }
179
195
  increaseBalance(state, validatorIndex, amount);
180
196
  }
181
- stateElectra.pendingDeposits = ssz.electra.PendingDeposits.defaultViewDU();
197
+ if (fork >= ForkSeq.gloas) {
198
+ (state as CachedBeaconStateGloas).pendingDeposits = ssz.gloas.PendingDeposits.defaultViewDU();
199
+ } else {
200
+ stateElectra.pendingDeposits = ssz.electra.PendingDeposits.defaultViewDU();
201
+ }
182
202
  }
183
203
 
184
204
  // Process activations
@@ -316,7 +336,9 @@ export function initializeBeaconStateFromEth1(
316
336
  stateElectra.latestExecutionPayloadHeader =
317
337
  (executionPayloadHeader as CompositeViewDU<typeof ssz.electra.ExecutionPayloadHeader>) ??
318
338
  ssz.electra.ExecutionPayloadHeader.defaultViewDU();
319
- stateElectra.depositRequestsStartIndex = UNSET_DEPOSIT_REQUESTS_START_INDEX;
339
+ if (fork < ForkSeq.heze) {
340
+ stateElectra.depositRequestsStartIndex = UNSET_DEPOSIT_REQUESTS_START_INDEX;
341
+ }
320
342
  }
321
343
 
322
344
  if (fork >= ForkSeq.fulu) {
@@ -326,6 +348,10 @@ export function initializeBeaconStateFromEth1(
326
348
  stateFulu.latestExecutionPayloadHeader =
327
349
  (executionPayloadHeader as CompositeViewDU<typeof ssz.fulu.ExecutionPayloadHeader>) ??
328
350
  ssz.fulu.ExecutionPayloadHeader.defaultViewDU();
351
+ if (fork < ForkSeq.heze) {
352
+ // Fulu retires the legacy bridge after the genesis deposits.
353
+ stateFulu.depositRequestsStartIndex = BigInt(stateFulu.eth1DepositIndex);
354
+ }
329
355
  }
330
356
 
331
357
  if (fork >= ForkSeq.gloas) {