33from eth2spec .test .helpers .epoch_processing import (
44 run_epoch_processing_with , run_epoch_processing_to
55)
6- from eth2spec .test .helpers .forks import is_post_altair , is_post_bellatrix
6+ from eth2spec .test .helpers .forks import (
7+ is_post_altair ,
8+ is_post_bellatrix ,
9+ is_post_electra ,
10+ )
711from eth2spec .test .helpers .random import randomize_state
812from eth2spec .test .helpers .state import has_active_balance_differential
913from eth2spec .test .helpers .voluntary_exits import get_unslashed_exited_validators
@@ -40,6 +44,18 @@ def get_slashing_multiplier(spec):
4044 return spec .PROPORTIONAL_SLASHING_MULTIPLIER
4145
4246
47+ def _compute_expected_correlation_penalty (spec , effective_balance , total_slashed_balance , total_balance ):
48+ if is_post_electra (spec ):
49+ return ((get_slashing_multiplier (spec ) * total_slashed_balance )
50+ // (total_balance // spec .EFFECTIVE_BALANCE_INCREMENT )
51+ * (effective_balance // spec .EFFECTIVE_BALANCE_INCREMENT ))
52+ else :
53+ return (effective_balance // spec .EFFECTIVE_BALANCE_INCREMENT
54+ * (get_slashing_multiplier (spec ) * total_slashed_balance )
55+ // total_balance
56+ * spec .EFFECTIVE_BALANCE_INCREMENT )
57+
58+
4359def _setup_process_slashings_test (spec , state , not_slashable_set = set ()):
4460 # Slashed count to ensure that enough validators are slashed to induce maximum penalties
4561 slashed_count = min (
@@ -99,7 +115,8 @@ def test_minimal_penalty(spec, state):
99115 #
100116
101117 # Just the bare minimum for this one validator
102- state .balances [0 ] = state .validators [0 ].effective_balance = spec .config .EJECTION_BALANCE
118+ state .balances [0 ] = state .validators [0 ].effective_balance = (
119+ spec .config .EJECTION_BALANCE + spec .EFFECTIVE_BALANCE_INCREMENT )
103120 # All the other validators get the maximum.
104121 for i in range (1 , len (state .validators )):
105122 state .validators [i ].effective_balance = state .balances [i ] = spec .MAX_EFFECTIVE_BALANCE
@@ -119,15 +136,10 @@ def test_minimal_penalty(spec, state):
119136 spec .process_slashings (state )
120137 yield 'post' , state
121138
122- expected_penalty = (
123- state .validators [0 ].effective_balance // spec .EFFECTIVE_BALANCE_INCREMENT
124- * (get_slashing_multiplier (spec ) * total_penalties )
125- // total_balance
126- * spec .EFFECTIVE_BALANCE_INCREMENT
127- )
139+ expected_penalty = _compute_expected_correlation_penalty (
140+ spec , state .validators [0 ].effective_balance , total_penalties , total_balance )
128141
129- assert expected_penalty == 0
130- assert state .balances [0 ] == pre_slash_balances [0 ]
142+ assert state .balances [0 ] == pre_slash_balances [0 ] - expected_penalty
131143
132144
133145@with_all_phases
@@ -181,12 +193,8 @@ def test_scaled_penalties(spec, state):
181193
182194 for i in slashed_indices :
183195 v = state .validators [i ]
184- expected_penalty = (
185- v .effective_balance // spec .EFFECTIVE_BALANCE_INCREMENT
186- * (get_slashing_multiplier (spec ) * total_penalties )
187- // (total_balance )
188- * spec .EFFECTIVE_BALANCE_INCREMENT
189- )
196+ expected_penalty = _compute_expected_correlation_penalty (
197+ spec , v .effective_balance , total_penalties , total_balance )
190198 assert state .balances [i ] == pre_slash_balances [i ] - expected_penalty
191199
192200
0 commit comments