Skip to content

Commit

Permalink
Fix UJ (#13688)
Browse files Browse the repository at this point in the history
* Fix UJ

* gate slashed

* don't filter slashed for active balance

* don't overflow

* fix tests

* fix tests

(cherry picked from commit b0a2115)
  • Loading branch information
potuz authored and prestonvanloon committed Mar 6, 2024
1 parent 6ec456d commit 89e2173
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
4 changes: 2 additions & 2 deletions beacon-chain/state/state-native/getters_participation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestState_UnrealizedCheckpointBalances(t *testing.T) {
balances[i] = params.BeaconConfig().MaxEffectiveBalance
}
base := &ethpb.BeaconStateAltair{
Slot: 2,
Slot: 66,
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),

Validators: validators,
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestState_UnrealizedCheckpointBalances(t *testing.T) {
require.NoError(t, err)
active, previous, current, err = state.UnrealizedCheckpointBalances()
require.NoError(t, err)
require.Equal(t, allActive-params.BeaconConfig().MaxEffectiveBalance, active)
require.Equal(t, allActive, active)
require.Equal(t, uint64(0), current)
require.Equal(t, params.BeaconConfig().MaxEffectiveBalance, previous)

Expand Down
28 changes: 16 additions & 12 deletions beacon-chain/state/stateutil/unrealized_justification.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,27 @@ func UnrealizedCheckpointBalances(cp, pp []byte, validators []*ethpb.Validator,

var err error
for i, v := range validators {
active := v.ActivationEpoch <= currentEpoch && currentEpoch < v.ExitEpoch
if active && !v.Slashed {
activeCurrent := v.ActivationEpoch <= currentEpoch && currentEpoch < v.ExitEpoch
if activeCurrent {
activeBalance, err = math.Add64(activeBalance, v.EffectiveBalance)
if err != nil {
return 0, 0, 0, err
}
if ((cp[i] >> targetIdx) & 1) == 1 {
currentTarget, err = math.Add64(currentTarget, v.EffectiveBalance)
if err != nil {
return 0, 0, 0, err
}
}
if v.Slashed {
continue
}
if activeCurrent && ((cp[i]>>targetIdx)&1) == 1 {
currentTarget, err = math.Add64(currentTarget, v.EffectiveBalance)
if err != nil {
return 0, 0, 0, err
}
if ((pp[i] >> targetIdx) & 1) == 1 {
prevTarget, err = math.Add64(prevTarget, v.EffectiveBalance)
if err != nil {
return 0, 0, 0, err
}
}
activePrevious := v.ActivationEpoch+1 <= currentEpoch && currentEpoch <= v.ExitEpoch
if activePrevious && ((pp[i]>>targetIdx)&1) == 1 {
prevTarget, err = math.Add64(prevTarget, v.EffectiveBalance)
if err != nil {
return 0, 0, 0, err
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func TestState_UnrealizedCheckpointBalances(t *testing.T) {
validators[1].Slashed = true
active, previous, current, err := UnrealizedCheckpointBalances(cp, pp, validators, 1)
require.NoError(tt, err)
expectedActive -= params.BeaconConfig().MaxEffectiveBalance
require.Equal(tt, expectedActive, active)
require.Equal(tt, params.BeaconConfig().MaxEffectiveBalance-params.BeaconConfig().MinDepositAmount, current)
require.Equal(tt, 2*params.BeaconConfig().MaxEffectiveBalance, previous)
Expand Down

0 comments on commit 89e2173

Please sign in to comment.