1
1
import { BeaconConfig , ChainForkConfig } from "@lodestar/config" ;
2
- import { EFFECTIVE_BALANCE_INCREMENT , MAX_DEPOSITS , MAX_EFFECTIVE_BALANCE , SLOTS_PER_EPOCH } from "@lodestar/params" ;
2
+ import {
3
+ EFFECTIVE_BALANCE_INCREMENT ,
4
+ MAX_DEPOSITS ,
5
+ MAX_EFFECTIVE_BALANCE ,
6
+ SLOTS_PER_EPOCH ,
7
+ isForkPostElectra ,
8
+ } from "@lodestar/params" ;
3
9
import { Epoch , Root } from "@lodestar/types" ;
4
10
import { ssz } from "@lodestar/types" ;
5
11
import { Checkpoint } from "@lodestar/types/phase0" ;
@@ -8,7 +14,12 @@ import {ZERO_HASH} from "../constants/constants.js";
8
14
import { BeaconStateAllForks , CachedBeaconStateAllForks } from "../types.js" ;
9
15
import { computeCheckpointEpochAtStateSlot , computeEpochAtSlot , getCurrentEpoch } from "./epoch.js" ;
10
16
import { getCurrentSlot } from "./slot.js" ;
11
- import { getActiveValidatorIndices , getChurnLimit } from "./validator.js" ;
17
+ import {
18
+ getActiveValidatorIndices ,
19
+ getBalanceChurnLimit ,
20
+ getBalanceChurnLimitFromCache ,
21
+ getChurnLimit ,
22
+ } from "./validator.js" ;
12
23
13
24
export const ETH_TO_GWEI = 10 ** 9 ;
14
25
const SAFETY_DECAY = 10 ;
@@ -37,12 +48,20 @@ export function computeWeakSubjectivityPeriodCachedState(
37
48
state : CachedBeaconStateAllForks
38
49
) : number {
39
50
const activeValidatorCount = state . epochCtx . currentShuffling . activeIndices . length ;
40
- return computeWeakSubjectivityPeriodFromConstituents (
41
- activeValidatorCount ,
42
- state . epochCtx . totalActiveBalanceIncrements ,
43
- getChurnLimit ( config , activeValidatorCount ) ,
44
- config . MIN_VALIDATOR_WITHDRAWABILITY_DELAY
45
- ) ;
51
+ const fork = state . config . getForkName ( state . slot ) ;
52
+
53
+ return isForkPostElectra ( fork )
54
+ ? computeWeakSubjectivityPeriodFromConstituentsElectra (
55
+ state . epochCtx . totalActiveBalanceIncrements ,
56
+ getBalanceChurnLimitFromCache ( state . epochCtx ) ,
57
+ config . MIN_VALIDATOR_WITHDRAWABILITY_DELAY
58
+ )
59
+ : computeWeakSubjectivityPeriodFromConstituentsPhase0 (
60
+ activeValidatorCount ,
61
+ state . epochCtx . totalActiveBalanceIncrements ,
62
+ getChurnLimit ( config , activeValidatorCount ) ,
63
+ config . MIN_VALIDATOR_WITHDRAWABILITY_DELAY
64
+ ) ;
46
65
}
47
66
48
67
/**
@@ -52,22 +71,35 @@ export function computeWeakSubjectivityPeriodCachedState(
52
71
export function computeWeakSubjectivityPeriod ( config : ChainForkConfig , state : BeaconStateAllForks ) : number {
53
72
const activeIndices = getActiveValidatorIndices ( state , getCurrentEpoch ( state ) ) ;
54
73
const validators = state . validators . getAllReadonlyValues ( ) ;
74
+ const fork = config . getForkName ( state . slot ) ;
75
+
55
76
let totalActiveBalanceIncrements = 0 ;
56
77
for ( const index of activeIndices ) {
57
78
totalActiveBalanceIncrements += Math . floor ( validators [ index ] . effectiveBalance / EFFECTIVE_BALANCE_INCREMENT ) ;
58
79
}
59
80
if ( totalActiveBalanceIncrements <= 1 ) {
60
81
totalActiveBalanceIncrements = 1 ;
61
82
}
62
- return computeWeakSubjectivityPeriodFromConstituents (
63
- activeIndices . length ,
64
- totalActiveBalanceIncrements ,
65
- getChurnLimit ( config , activeIndices . length ) ,
66
- config . MIN_VALIDATOR_WITHDRAWABILITY_DELAY
67
- ) ;
83
+
84
+ return isForkPostElectra ( fork )
85
+ ? computeWeakSubjectivityPeriodFromConstituentsElectra (
86
+ totalActiveBalanceIncrements ,
87
+ getBalanceChurnLimit (
88
+ totalActiveBalanceIncrements ,
89
+ config . CHURN_LIMIT_QUOTIENT ,
90
+ config . MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA
91
+ ) ,
92
+ config . MIN_VALIDATOR_WITHDRAWABILITY_DELAY
93
+ )
94
+ : computeWeakSubjectivityPeriodFromConstituentsPhase0 (
95
+ activeIndices . length ,
96
+ totalActiveBalanceIncrements ,
97
+ getChurnLimit ( config , activeIndices . length ) ,
98
+ config . MIN_VALIDATOR_WITHDRAWABILITY_DELAY
99
+ ) ;
68
100
}
69
101
70
- export function computeWeakSubjectivityPeriodFromConstituents (
102
+ export function computeWeakSubjectivityPeriodFromConstituentsPhase0 (
71
103
activeValidatorCount : number ,
72
104
totalBalanceByIncrement : number ,
73
105
churnLimit : number ,
@@ -97,6 +129,20 @@ export function computeWeakSubjectivityPeriodFromConstituents(
97
129
return wsPeriod ;
98
130
}
99
131
132
+ export function computeWeakSubjectivityPeriodFromConstituentsElectra (
133
+ totalBalanceByIncrement : number ,
134
+ // Note this is not the same as churnLimit in `computeWeakSubjectivityPeriodFromConstituentsPhase0`
135
+ balanceChurnLimit : number ,
136
+ minWithdrawabilityDelay : number
137
+ ) : number {
138
+ // Keep t as increment for now. Multiply final result by EFFECTIVE_BALANCE_INCREMENT
139
+ const t = totalBalanceByIncrement ;
140
+ const delta = balanceChurnLimit ;
141
+ const epochsForValidatorSetChurn = Math . floor ( ( ( SAFETY_DECAY * t ) / ( 2 * delta * 100 ) ) * EFFECTIVE_BALANCE_INCREMENT ) ;
142
+
143
+ return minWithdrawabilityDelay + epochsForValidatorSetChurn ;
144
+ }
145
+
100
146
export function getLatestBlockRoot ( state : BeaconStateAllForks ) : Root {
101
147
const header = ssz . phase0 . BeaconBlockHeader . clone ( state . latestBlockHeader ) ;
102
148
if ( ssz . Root . equals ( header . stateRoot , ZERO_HASH ) ) {
0 commit comments