@lodestar/state-transition 1.51.0-dev.2593063a82 → 1.51.0-dev.4157a0dec4

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 (49) hide show
  1. package/lib/block/processAttestationsAltair.d.ts.map +1 -1
  2. package/lib/block/processAttestationsAltair.js +3 -1
  3. package/lib/block/processAttestationsAltair.js.map +1 -1
  4. package/lib/cache/epochCache.d.ts +3 -0
  5. package/lib/cache/epochCache.d.ts.map +1 -1
  6. package/lib/cache/epochCache.js +11 -4
  7. package/lib/cache/epochCache.js.map +1 -1
  8. package/lib/cache/epochTransitionCache.d.ts.map +1 -1
  9. package/lib/cache/epochTransitionCache.js +2 -1
  10. package/lib/cache/epochTransitionCache.js.map +1 -1
  11. package/lib/epoch/getRewardsAndPenalties.d.ts.map +1 -1
  12. package/lib/epoch/getRewardsAndPenalties.js +9 -2
  13. package/lib/epoch/getRewardsAndPenalties.js.map +1 -1
  14. package/lib/rewards/attestationsRewards.d.ts.map +1 -1
  15. package/lib/rewards/attestationsRewards.js +10 -3
  16. package/lib/rewards/attestationsRewards.js.map +1 -1
  17. package/lib/util/altair.d.ts +3 -1
  18. package/lib/util/altair.d.ts.map +1 -1
  19. package/lib/util/altair.js +7 -4
  20. package/lib/util/altair.js.map +1 -1
  21. package/lib/util/clock.js +1 -1
  22. package/lib/util/clock.js.map +1 -1
  23. package/lib/util/slot.d.ts +15 -1
  24. package/lib/util/slot.d.ts.map +1 -1
  25. package/lib/util/slot.js +66 -6
  26. package/lib/util/slot.js.map +1 -1
  27. package/lib/util/syncCommittee.d.ts +4 -3
  28. package/lib/util/syncCommittee.d.ts.map +1 -1
  29. package/lib/util/syncCommittee.js +9 -7
  30. package/lib/util/syncCommittee.js.map +1 -1
  31. package/lib/util/validator.d.ts +1 -1
  32. package/lib/util/validator.d.ts.map +1 -1
  33. package/lib/util/validator.js +32 -11
  34. package/lib/util/validator.js.map +1 -1
  35. package/lib/util/weakSubjectivity.d.ts.map +1 -1
  36. package/lib/util/weakSubjectivity.js +4 -3
  37. package/lib/util/weakSubjectivity.js.map +1 -1
  38. package/package.json +7 -7
  39. package/src/block/processAttestationsAltair.ts +4 -1
  40. package/src/cache/epochCache.ts +28 -4
  41. package/src/cache/epochTransitionCache.ts +2 -1
  42. package/src/epoch/getRewardsAndPenalties.ts +11 -1
  43. package/src/rewards/attestationsRewards.ts +12 -1
  44. package/src/util/altair.ts +15 -4
  45. package/src/util/clock.ts +1 -1
  46. package/src/util/slot.ts +72 -7
  47. package/src/util/syncCommittee.ts +21 -9
  48. package/src/util/validator.ts +47 -20
  49. package/src/util/weakSubjectivity.ts +3 -3
package/src/util/clock.ts CHANGED
@@ -133,7 +133,7 @@ export class Clock implements IClock {
133
133
  if (timeItem === TimeItem.Slot) {
134
134
  if (nextSlot > slot + 1) {
135
135
  // It's not very likely that we skip more than one slot as HTTP timeout is set
136
- // 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.
137
137
  this.logger.warn("Skipped slot due to task taking more than one slot to run", {
138
138
  skippedSlot: slot + 1,
139
139
  });
package/src/util/slot.ts CHANGED
@@ -1,27 +1,92 @@
1
1
  import {ChainConfig, ChainForkConfig} from "@lodestar/config";
2
- import {GENESIS_SLOT} from "@lodestar/params";
2
+ import {GENESIS_EPOCH, GENESIS_SLOT} from "@lodestar/params";
3
3
  import {Epoch, Slot, TimeSeconds} from "@lodestar/types";
4
4
  import {computeEpochAtSlot, computeStartSlotAtEpoch} from "./epoch.js";
5
5
 
6
+ /**
7
+ * Slot durations with their activation epochs, derived from the configuration.
8
+ * A fork that changes the slot duration must add an entry here and in `ForkConfig.getSlotDurationMs`.
9
+ */
10
+ export function getSlotDurations(config: ChainConfig): [Epoch, number][] {
11
+ const durations: [Epoch, number][] = [[GENESIS_EPOCH, config.SLOT_DURATION_MS]];
12
+ if (config.HEZE_FORK_EPOCH !== Infinity) {
13
+ durations.push([config.HEZE_FORK_EPOCH, config.SLOT_DURATION_MS_HEZE]);
14
+ }
15
+ return durations;
16
+ }
17
+
18
+ /** Slot duration in ms in effect at a given epoch */
19
+ export function getSlotDurationMsAtEpoch(config: ChainConfig, epoch: Epoch): number {
20
+ let slotDurationMs = config.SLOT_DURATION_MS;
21
+ for (const [forkEpoch, forkSlotDurationMs] of getSlotDurations(config)) {
22
+ if (epoch >= forkEpoch) {
23
+ slotDurationMs = forkSlotDurationMs;
24
+ }
25
+ }
26
+ return slotDurationMs;
27
+ }
28
+
6
29
  /** Slot duration in ms for a given slot */
7
30
  export function getSlotDurationMs(config: ChainForkConfig, slot: Slot): number {
8
31
  return config.getSlotDurationMs(config.getForkName(slot));
9
32
  }
10
33
 
34
+ export function computeTimeAtSlotMs(config: ChainConfig, slot: Slot, genesisTimeMs: number): number {
35
+ let endSlot = slot;
36
+ let timeMs = genesisTimeMs;
37
+ for (const [forkEpoch, slotDurationMs] of getSlotDurations(config).reverse()) {
38
+ const forkSlot = computeStartSlotAtEpoch(forkEpoch);
39
+ if (forkSlot < endSlot) {
40
+ timeMs += (endSlot - forkSlot) * slotDurationMs;
41
+ endSlot = forkSlot;
42
+ }
43
+ }
44
+ // Pre-genesis slots extrapolate linearly so the clock keeps ticking before genesis
45
+ return timeMs + (endSlot - GENESIS_SLOT) * getSlotDurationMsAtEpoch(config, GENESIS_EPOCH);
46
+ }
47
+
48
+ export function computeTimeAtSlot(config: ChainConfig, slot: Slot, genesisTime: TimeSeconds): TimeSeconds {
49
+ return computeTimeAtSlotMs(config, slot, genesisTime * 1000) / 1000;
50
+ }
51
+
52
+ export function computeSlotAtTimeMs(config: ChainConfig, timeMs: number, genesisTimeMs: number): Slot {
53
+ let startSlot = GENESIS_SLOT;
54
+ let startTimeMs = genesisTimeMs;
55
+ let slotDurationMs = getSlotDurationMsAtEpoch(config, GENESIS_EPOCH);
56
+ for (const [forkEpoch, forkSlotDurationMs] of getSlotDurations(config)) {
57
+ const forkSlot = computeStartSlotAtEpoch(forkEpoch);
58
+ const forkTimeMs = computeTimeAtSlotMs(config, forkSlot, genesisTimeMs);
59
+ if (timeMs >= forkTimeMs) {
60
+ startSlot = forkSlot;
61
+ startTimeMs = forkTimeMs;
62
+ slotDurationMs = forkSlotDurationMs;
63
+ }
64
+ }
65
+ return startSlot + Math.floor((timeMs - startTimeMs) / slotDurationMs);
66
+ }
67
+
11
68
  export function getSlotsSinceGenesis(config: ChainConfig, genesisTime: TimeSeconds): Slot {
12
- const diffInSeconds = Date.now() / 1000 - genesisTime;
13
- return Math.floor(diffInSeconds / (config.SLOT_DURATION_MS / 1000));
69
+ return computeSlotAtTimeMs(config, Date.now(), genesisTime * 1000) - GENESIS_SLOT;
14
70
  }
15
71
 
16
72
  export function getCurrentSlot(config: ChainConfig, genesisTime: TimeSeconds): Slot {
17
73
  return GENESIS_SLOT + getSlotsSinceGenesis(config, genesisTime);
18
74
  }
19
75
 
76
+ /**
77
+ * Start epoch of the blob data retention window, preserving its wall-clock length
78
+ * across slot duration changes
79
+ */
80
+ export function getBlobDataRetentionStartEpoch(config: ChainConfig, epoch: Epoch): Epoch {
81
+ const windowMs = config.MIN_BLOB_DATA_RETENTION_MS;
82
+ const currentStartMs = computeTimeAtSlotMs(config, computeStartSlotAtEpoch(epoch), 0);
83
+ if (currentStartMs < windowMs) {
84
+ return GENESIS_EPOCH;
85
+ }
86
+ return computeEpochAtSlot(computeSlotAtTimeMs(config, currentStartMs - windowMs, 0));
87
+ }
88
+
20
89
  export function computeSlotsSinceEpochStart(slot: Slot, epoch?: Epoch): Slot {
21
90
  const computeEpoch = epoch ?? computeEpochAtSlot(slot);
22
91
  return slot - computeStartSlotAtEpoch(computeEpoch);
23
92
  }
24
-
25
- export function computeTimeAtSlot(config: ChainConfig, slot: Slot, genesisTime: TimeSeconds): TimeSeconds {
26
- return genesisTime + slot * (config.SLOT_DURATION_MS / 1000);
27
- }
@@ -1,18 +1,21 @@
1
1
  import {aggregateSerializedPublicKeys} from "@chainsafe/lodestar-z/blst";
2
+ import {ChainConfig} from "@lodestar/config";
2
3
  import {
3
4
  BASE_REWARD_FACTOR,
4
5
  EFFECTIVE_BALANCE_INCREMENT,
5
6
  ForkSeq,
7
+ GENESIS_EPOCH,
6
8
  SLOTS_PER_EPOCH,
7
9
  SYNC_COMMITTEE_SIZE,
8
10
  SYNC_REWARD_WEIGHT,
9
11
  WEIGHT_DENOMINATOR,
10
12
  } from "@lodestar/params";
11
- import {altair} from "@lodestar/types";
13
+ import {Epoch, altair} from "@lodestar/types";
12
14
  import {bigIntSqrt} from "@lodestar/utils";
13
15
  import {EffectiveBalanceIncrements} from "../cache/effectiveBalanceIncrements.js";
14
16
  import {BeaconStateAllForks} from "../types.js";
15
17
  import {getNextSyncCommitteeIndices} from "./seed.js";
18
+ import {getSlotDurationMsAtEpoch} from "./slot.js";
16
19
 
17
20
  /**
18
21
  * Return the sync committee for a given state and epoch.
@@ -45,11 +48,12 @@ export function getNextSyncCommittee(
45
48
  /**
46
49
  * Same logic in https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.5/specs/altair/beacon-chain.md#sync-committee-processing
47
50
  */
48
- export function computeSyncParticipantReward(totalActiveBalanceIncrements: number): number {
49
- const totalActiveBalance = BigInt(totalActiveBalanceIncrements) * BigInt(EFFECTIVE_BALANCE_INCREMENT);
50
- const baseRewardPerIncrement = Math.floor(
51
- (EFFECTIVE_BALANCE_INCREMENT * BASE_REWARD_FACTOR) / Number(bigIntSqrt(totalActiveBalance))
52
- );
51
+ export function computeSyncParticipantReward(
52
+ config: ChainConfig,
53
+ epoch: Epoch,
54
+ totalActiveBalanceIncrements: number
55
+ ): number {
56
+ const baseRewardPerIncrement = computeBaseRewardPerIncrement(config, epoch, totalActiveBalanceIncrements);
53
57
  const totalBaseRewards = baseRewardPerIncrement * totalActiveBalanceIncrements;
54
58
  const maxParticipantRewards = Math.floor(
55
59
  Math.floor((totalBaseRewards * SYNC_REWARD_WEIGHT) / WEIGHT_DENOMINATOR) / SLOTS_PER_EPOCH
@@ -61,9 +65,17 @@ export function computeSyncParticipantReward(totalActiveBalanceIncrements: numbe
61
65
  * Before we manage bigIntSqrt(totalActiveStake) as BigInt and return BigInt.
62
66
  * bigIntSqrt(totalActiveStake) should fit a number (2 ** 53 -1 max)
63
67
  **/
64
- export function computeBaseRewardPerIncrement(totalActiveStakeByIncrement: number): number {
68
+ export function computeBaseRewardPerIncrement(
69
+ config: ChainConfig,
70
+ epoch: Epoch,
71
+ totalActiveStakeByIncrement: number
72
+ ): number {
73
+ // Divide last so the exact slot duration ratio applies
74
+ const numerator = Math.floor(
75
+ (EFFECTIVE_BALANCE_INCREMENT * BASE_REWARD_FACTOR * getSlotDurationMsAtEpoch(config, epoch)) /
76
+ getSlotDurationMsAtEpoch(config, GENESIS_EPOCH)
77
+ );
65
78
  return Math.floor(
66
- (EFFECTIVE_BALANCE_INCREMENT * BASE_REWARD_FACTOR) /
67
- Number(bigIntSqrt(BigInt(totalActiveStakeByIncrement) * BigInt(EFFECTIVE_BALANCE_INCREMENT)))
79
+ numerator / Number(bigIntSqrt(BigInt(totalActiveStakeByIncrement) * BigInt(EFFECTIVE_BALANCE_INCREMENT)))
68
80
  );
69
81
  }
@@ -2,6 +2,7 @@ import {ChainForkConfig} from "@lodestar/config";
2
2
  import {
3
3
  EFFECTIVE_BALANCE_INCREMENT,
4
4
  ForkSeq,
5
+ GENESIS_EPOCH,
5
6
  MAX_EFFECTIVE_BALANCE,
6
7
  MAX_EFFECTIVE_BALANCE_ELECTRA,
7
8
  MIN_ACTIVATION_BALANCE,
@@ -11,6 +12,7 @@ import {intDiv} from "@lodestar/utils";
11
12
  import {BeaconStateAllForks, CachedBeaconStateElectra, CachedBeaconStateGloas, EpochCache} from "../types.js";
12
13
  import {hasEth1WithdrawalCredential} from "./capella.js";
13
14
  import {hasCompoundingWithdrawalCredential, hasExecutionWithdrawalCredential} from "./electra.js";
15
+ import {getSlotDurationMsAtEpoch} from "./slot.js";
14
16
 
15
17
  /**
16
18
  * Check if [[validator]] is active
@@ -93,23 +95,35 @@ export function getActivationExitChurnLimit(epochCtx: EpochCache): number {
93
95
  * https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.6/specs/gloas/beacon-chain.md#new-get_activation_churn_limit
94
96
  */
95
97
  export function getActivationChurnLimit(epochCtx: EpochCache): number {
96
- const churn = getBalanceChurnLimit(
97
- epochCtx.totalActiveBalanceIncrements,
98
- epochCtx.config.CHURN_LIMIT_QUOTIENT_GLOAS,
99
- epochCtx.config.MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA
98
+ const {config, epoch, totalActiveBalanceIncrements} = epochCtx;
99
+ const churnByBalance = Math.floor(
100
+ (totalActiveBalanceIncrements / config.CHURN_LIMIT_QUOTIENT_GLOAS) * EFFECTIVE_BALANCE_INCREMENT
101
+ );
102
+ const churn = Math.min(
103
+ config.MAX_PER_EPOCH_ACTIVATION_CHURN_LIMIT_GLOAS,
104
+ Math.max(churnByBalance, config.MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA)
100
105
  );
101
- return Math.min(epochCtx.config.MAX_PER_EPOCH_ACTIVATION_CHURN_LIMIT_GLOAS, churn);
106
+ // Scale by the slot duration ratio, rounding to the increment once after scaling
107
+ const scaled = Math.floor(
108
+ (churn * getSlotDurationMsAtEpoch(config, epoch)) / getSlotDurationMsAtEpoch(config, GENESIS_EPOCH)
109
+ );
110
+ return scaled - (scaled % EFFECTIVE_BALANCE_INCREMENT);
102
111
  }
103
112
 
104
113
  /**
105
114
  * https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.6/specs/gloas/beacon-chain.md#new-get_exit_churn_limit
106
115
  */
107
116
  export function getExitChurnLimit(epochCtx: EpochCache): number {
108
- return getBalanceChurnLimit(
109
- epochCtx.totalActiveBalanceIncrements,
110
- epochCtx.config.CHURN_LIMIT_QUOTIENT_GLOAS,
111
- epochCtx.config.MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA
117
+ const {config, epoch, totalActiveBalanceIncrements} = epochCtx;
118
+ const churnByBalance = Math.floor(
119
+ (totalActiveBalanceIncrements / config.CHURN_LIMIT_QUOTIENT_GLOAS) * EFFECTIVE_BALANCE_INCREMENT
120
+ );
121
+ const churn = Math.max(churnByBalance, config.MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA);
122
+ // Scale by the slot duration ratio, rounding to the increment once after scaling
123
+ const scaled = Math.floor(
124
+ (churn * getSlotDurationMsAtEpoch(config, epoch)) / getSlotDurationMsAtEpoch(config, GENESIS_EPOCH)
112
125
  );
126
+ return scaled - (scaled % EFFECTIVE_BALANCE_INCREMENT);
113
127
  }
114
128
 
115
129
  /**
@@ -118,12 +132,16 @@ export function getExitChurnLimit(epochCtx: EpochCache): number {
118
132
  */
119
133
  export function getConsolidationChurnLimit(fork: ForkSeq, epochCtx: EpochCache): number {
120
134
  if (fork >= ForkSeq.gloas) {
121
- // No MIN floor — pass 0 so getBalanceChurnLimit's max(churn, min) is a no-op.
122
- return getBalanceChurnLimit(
123
- epochCtx.totalActiveBalanceIncrements,
124
- epochCtx.config.CONSOLIDATION_CHURN_LIMIT_QUOTIENT,
125
- 0
135
+ const {config, epoch, totalActiveBalanceIncrements} = epochCtx;
136
+ // No MIN floor in gloas
137
+ const churn = Math.floor(
138
+ (totalActiveBalanceIncrements / config.CONSOLIDATION_CHURN_LIMIT_QUOTIENT) * EFFECTIVE_BALANCE_INCREMENT
126
139
  );
140
+ // Scale by the slot duration ratio, rounding to the increment once after scaling
141
+ const scaled = Math.floor(
142
+ (churn * getSlotDurationMsAtEpoch(config, epoch)) / getSlotDurationMsAtEpoch(config, GENESIS_EPOCH)
143
+ );
144
+ return scaled - (scaled % EFFECTIVE_BALANCE_INCREMENT);
127
145
  }
128
146
  return getBalanceChurnLimitFromCache(epochCtx) - getActivationExitChurnLimit(epochCtx);
129
147
  }
@@ -131,17 +149,26 @@ export function getConsolidationChurnLimit(fork: ForkSeq, epochCtx: EpochCache):
131
149
  // NOTE: duplicates the quotient/min choices in get{Exit,Activation,Consolidation}ChurnLimit.
132
150
  export function getGloasChurnLimits(
133
151
  config: ChainForkConfig,
152
+ epoch: Epoch,
134
153
  totalActiveBalanceIncrements: number
135
154
  ): {exit: number; activation: number; consolidation: number} {
136
- const exit = getBalanceChurnLimit(
137
- totalActiveBalanceIncrements,
138
- config.CHURN_LIMIT_QUOTIENT_GLOAS,
155
+ const ratioNumerator = getSlotDurationMsAtEpoch(config, epoch);
156
+ const ratioDenominator = getSlotDurationMsAtEpoch(config, GENESIS_EPOCH);
157
+ const exitUnscaled = Math.max(
158
+ Math.floor((totalActiveBalanceIncrements / config.CHURN_LIMIT_QUOTIENT_GLOAS) * EFFECTIVE_BALANCE_INCREMENT),
139
159
  config.MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA
140
160
  );
161
+ const activationUnscaled = Math.min(config.MAX_PER_EPOCH_ACTIVATION_CHURN_LIMIT_GLOAS, exitUnscaled);
162
+ const consolidationUnscaled = Math.floor(
163
+ (totalActiveBalanceIncrements / config.CONSOLIDATION_CHURN_LIMIT_QUOTIENT) * EFFECTIVE_BALANCE_INCREMENT
164
+ );
165
+ const exit = Math.floor((exitUnscaled * ratioNumerator) / ratioDenominator);
166
+ const activation = Math.floor((activationUnscaled * ratioNumerator) / ratioDenominator);
167
+ const consolidation = Math.floor((consolidationUnscaled * ratioNumerator) / ratioDenominator);
141
168
  return {
142
- exit,
143
- activation: Math.min(config.MAX_PER_EPOCH_ACTIVATION_CHURN_LIMIT_GLOAS, exit),
144
- consolidation: getBalanceChurnLimit(totalActiveBalanceIncrements, config.CONSOLIDATION_CHURN_LIMIT_QUOTIENT, 0),
169
+ exit: exit - (exit % EFFECTIVE_BALANCE_INCREMENT),
170
+ activation: activation - (activation % EFFECTIVE_BALANCE_INCREMENT),
171
+ consolidation: consolidation - (consolidation % EFFECTIVE_BALANCE_INCREMENT),
145
172
  };
146
173
  }
147
174
 
@@ -2,7 +2,6 @@ import {ChainForkConfig} from "@lodestar/config";
2
2
  import {
3
3
  EFFECTIVE_BALANCE_INCREMENT,
4
4
  ForkSeq,
5
- GENESIS_SLOT,
6
5
  MAX_DEPOSITS,
7
6
  MAX_EFFECTIVE_BALANCE,
8
7
  SLOTS_PER_EPOCH,
@@ -13,6 +12,7 @@ import {Epoch, Root, Slot, TimeSeconds, ssz} from "@lodestar/types";
13
12
  import {ZERO_HASH} from "../constants/constants.js";
14
13
  import {BeaconStateAllForks, CachedBeaconStateAllForks} from "../types.js";
15
14
  import {computeCheckpointEpochAtStateSlot, computeEpochAtSlot, getCurrentEpoch} from "./epoch.js";
15
+ import {computeSlotAtTimeMs} from "./slot.js";
16
16
  import {StateBytesMetadata, getStateTypeFromBytes, scanActiveValidatorsFromStateBytes} from "./sszBytes.js";
17
17
  import {
18
18
  getActivationChurnLimit,
@@ -107,7 +107,7 @@ function computeWeakSubjectivityPeriodFromActiveValidators(
107
107
  ): number {
108
108
  const fork = config.getForkName(slot);
109
109
 
110
- const churnLimitsGloas = getGloasChurnLimits(config, totalActiveBalanceIncrements);
110
+ const churnLimitsGloas = getGloasChurnLimits(config, computeEpochAtSlot(slot), totalActiveBalanceIncrements);
111
111
 
112
112
  return isForkPostGloas(fork)
113
113
  ? computeWeakSubjectivityPeriodFromConstituentsGloas(
@@ -223,6 +223,6 @@ export function isWithinWeakSubjectivityPeriodFromSummary(
223
223
  summary: WeakSubjectivitySummary,
224
224
  now: TimeSeconds = Date.now() / 1000
225
225
  ): boolean {
226
- const clockSlot = GENESIS_SLOT + Math.floor((now - summary.genesisTime) / (config.SLOT_DURATION_MS / 1000));
226
+ const clockSlot = computeSlotAtTimeMs(config, now * 1000, summary.genesisTime * 1000);
227
227
  return computeEpochAtSlot(clockSlot) <= summary.checkpointEpoch + summary.period;
228
228
  }