Skip to content

Commit

Permalink
Check Unrealized Justification Balances In Spectests (#13710)
Browse files Browse the repository at this point in the history
* add them

* Ensure activation epoch does not overflow

* add them all in

* better check for overflow

* fix tests

* fix tests

---------

Co-authored-by: Potuz <[email protected]>
(cherry picked from commit 2616de1)
  • Loading branch information
nisdas authored and prestonvanloon committed Mar 8, 2024
1 parent 89e2173 commit 3d1d586
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 11 deletions.
6 changes: 3 additions & 3 deletions beacon-chain/state/state-native/getters_participation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func TestState_UnrealizedCheckpointBalances(t *testing.T) {
active, previous, current, err := state.UnrealizedCheckpointBalances()
require.NoError(t, err)
require.Equal(t, allActive, active)
require.Equal(t, uint64(0), current)
require.Equal(t, uint64(0), previous)
require.Equal(t, params.BeaconConfig().EffectiveBalanceIncrement, current)
require.Equal(t, params.BeaconConfig().EffectiveBalanceIncrement, previous)

// Add some votes in the last two epochs:
base.CurrentEpochParticipation[0] = 0xFF
Expand All @@ -58,7 +58,7 @@ func TestState_UnrealizedCheckpointBalances(t *testing.T) {
active, previous, current, err = state.UnrealizedCheckpointBalances()
require.NoError(t, err)
require.Equal(t, allActive, active)
require.Equal(t, uint64(0), current)
require.Equal(t, params.BeaconConfig().EffectiveBalanceIncrement, current)
require.Equal(t, params.BeaconConfig().MaxEffectiveBalance, previous)

}
17 changes: 16 additions & 1 deletion beacon-chain/state/stateutil/unrealized_justification.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,28 @@ func UnrealizedCheckpointBalances(cp, pp []byte, validators []*ethpb.Validator,
return 0, 0, 0, err
}
}
activePrevious := v.ActivationEpoch+1 <= currentEpoch && currentEpoch <= v.ExitEpoch
activePrevious := v.ActivationEpoch < 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
}
}
}
activeBalance, prevTarget, currentTarget = ensureLowerBound(activeBalance, prevTarget, currentTarget)
return activeBalance, prevTarget, currentTarget, nil
}

func ensureLowerBound(activeCurrEpoch, prevTargetAttested, currTargetAttested uint64) (uint64, uint64, uint64) {
ebi := params.BeaconConfig().EffectiveBalanceIncrement
if ebi > activeCurrEpoch {
activeCurrEpoch = ebi
}
if ebi > prevTargetAttested {
prevTargetAttested = ebi
}
if ebi > currTargetAttested {
currTargetAttested = ebi
}
return activeCurrEpoch, prevTargetAttested, currTargetAttested
}
12 changes: 6 additions & 6 deletions beacon-chain/state/stateutil/unrealized_justification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func TestState_UnrealizedCheckpointBalances(t *testing.T) {
active, previous, current, err := UnrealizedCheckpointBalances(cp, pp, validators, 0)
require.NoError(tt, err)
require.Equal(tt, expectedActive, active)
require.Equal(tt, uint64(0), current)
require.Equal(tt, uint64(0), previous)
require.Equal(tt, params.BeaconConfig().EffectiveBalanceIncrement, current)
require.Equal(tt, params.BeaconConfig().EffectiveBalanceIncrement, previous)
})

t.Run("bad votes in last two epochs", func(tt *testing.T) {
Expand All @@ -38,8 +38,8 @@ func TestState_UnrealizedCheckpointBalances(t *testing.T) {
active, previous, current, err := UnrealizedCheckpointBalances(cp, pp, validators, 1)
require.NoError(tt, err)
require.Equal(tt, expectedActive, active)
require.Equal(tt, uint64(0), current)
require.Equal(tt, uint64(0), previous)
require.Equal(tt, params.BeaconConfig().EffectiveBalanceIncrement, current)
require.Equal(tt, params.BeaconConfig().EffectiveBalanceIncrement, previous)
})

t.Run("two votes in last epoch", func(tt *testing.T) {
Expand All @@ -49,7 +49,7 @@ func TestState_UnrealizedCheckpointBalances(t *testing.T) {
require.NoError(tt, err)
require.Equal(tt, expectedActive, active)
require.Equal(tt, 2*params.BeaconConfig().MaxEffectiveBalance, current)
require.Equal(tt, uint64(0), previous)
require.Equal(tt, params.BeaconConfig().EffectiveBalanceIncrement, previous)
})

t.Run("two votes in previous epoch", func(tt *testing.T) {
Expand All @@ -58,7 +58,7 @@ func TestState_UnrealizedCheckpointBalances(t *testing.T) {
active, previous, current, err := UnrealizedCheckpointBalances(cp, pp, validators, 1)
require.NoError(tt, err)
require.Equal(tt, expectedActive, active)
require.Equal(tt, uint64(0), current)
require.Equal(tt, params.BeaconConfig().EffectiveBalanceIncrement, current)
require.Equal(tt, 2*params.BeaconConfig().MaxEffectiveBalance, previous)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func processJustificationAndFinalizationPrecomputeWrapper(t *testing.T, st state
require.NoError(t, err)
_, bp, err = altair.ProcessEpochParticipation(ctx, st, bp, vp)
require.NoError(t, err)
activeBal, targetPrevious, targetCurrent, err := st.UnrealizedCheckpointBalances()
require.NoError(t, err)
require.Equal(t, bp.ActiveCurrentEpoch, activeBal)
require.Equal(t, bp.CurrentEpochTargetAttested, targetCurrent)
require.Equal(t, bp.PrevEpochTargetAttested, targetPrevious)

st, err = precompute.ProcessJustificationAndFinalizationPreCompute(st, bp)
require.NoError(t, err, "Could not process justification")
Expand Down
7 changes: 7 additions & 0 deletions testing/spectest/shared/altair/rewards/rewards_penalties.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func runPrecomputeRewardsAndPenaltiesTest(t *testing.T, testFolderPath string) {
require.NoError(t, err)
vp, bp, err = altair.ProcessEpochParticipation(ctx, preBeaconState, bp, vp)
require.NoError(t, err)

activeBal, targetPrevious, targetCurrent, err := preBeaconState.UnrealizedCheckpointBalances()
require.NoError(t, err)
require.Equal(t, bp.ActiveCurrentEpoch, activeBal)
require.Equal(t, bp.CurrentEpochTargetAttested, targetCurrent)
require.Equal(t, bp.PrevEpochTargetAttested, targetPrevious)

deltas, err := altair.AttestationsDelta(preBeaconState, bp, vp)
require.NoError(t, err)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func processJustificationAndFinalizationPrecomputeWrapper(t *testing.T, st state
require.NoError(t, err)
_, bp, err = altair.ProcessEpochParticipation(ctx, st, bp, vp)
require.NoError(t, err)
activeBal, targetPrevious, targetCurrent, err := st.UnrealizedCheckpointBalances()
require.NoError(t, err)
require.Equal(t, bp.ActiveCurrentEpoch, activeBal)
require.Equal(t, bp.CurrentEpochTargetAttested, targetCurrent)
require.Equal(t, bp.PrevEpochTargetAttested, targetPrevious)

st, err = precompute.ProcessJustificationAndFinalizationPreCompute(st, bp)
require.NoError(t, err, "Could not process justification")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ func runPrecomputeRewardsAndPenaltiesTest(t *testing.T, testFolderPath string) {
require.NoError(t, err)
vp, bp, err = altair.ProcessEpochParticipation(ctx, preBeaconState, bp, vp)
require.NoError(t, err)

activeBal, targetPrevious, targetCurrent, err := preBeaconState.UnrealizedCheckpointBalances()
require.NoError(t, err)
require.Equal(t, bp.ActiveCurrentEpoch, activeBal)
require.Equal(t, bp.CurrentEpochTargetAttested, targetCurrent)
require.Equal(t, bp.PrevEpochTargetAttested, targetPrevious)

deltas, err := altair.AttestationsDelta(preBeaconState, bp, vp)
require.NoError(t, err)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func processJustificationAndFinalizationPrecomputeWrapper(t *testing.T, st state
require.NoError(t, err)
_, bp, err = altair.ProcessEpochParticipation(ctx, st, bp, vp)
require.NoError(t, err)
activeBal, targetPrevious, targetCurrent, err := st.UnrealizedCheckpointBalances()
require.NoError(t, err)
require.Equal(t, bp.ActiveCurrentEpoch, activeBal)
require.Equal(t, bp.CurrentEpochTargetAttested, targetCurrent)
require.Equal(t, bp.PrevEpochTargetAttested, targetPrevious)

st, err = precompute.ProcessJustificationAndFinalizationPreCompute(st, bp)
require.NoError(t, err, "Could not process justification")
Expand Down
7 changes: 7 additions & 0 deletions testing/spectest/shared/capella/rewards/rewards_penalties.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ func runPrecomputeRewardsAndPenaltiesTest(t *testing.T, testFolderPath string) {
require.NoError(t, err)
vp, bp, err = altair.ProcessEpochParticipation(ctx, preBeaconState, bp, vp)
require.NoError(t, err)

activeBal, targetPrevious, targetCurrent, err := preBeaconState.UnrealizedCheckpointBalances()
require.NoError(t, err)
require.Equal(t, bp.ActiveCurrentEpoch, activeBal)
require.Equal(t, bp.CurrentEpochTargetAttested, targetCurrent)
require.Equal(t, bp.PrevEpochTargetAttested, targetPrevious)

deltas, err := altair.AttestationsDelta(preBeaconState, bp, vp)
require.NoError(t, err)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func processJustificationAndFinalizationPrecomputeWrapper(t *testing.T, st state
require.NoError(t, err)
_, bp, err = altair.ProcessEpochParticipation(ctx, st, bp, vp)
require.NoError(t, err)

activeBal, targetPrevious, targetCurrent, err := st.UnrealizedCheckpointBalances()
require.NoError(t, err)
require.Equal(t, bp.ActiveCurrentEpoch, activeBal)
require.Equal(t, bp.CurrentEpochTargetAttested, targetCurrent)
require.Equal(t, bp.PrevEpochTargetAttested, targetPrevious)
st, err = precompute.ProcessJustificationAndFinalizationPreCompute(st, bp)
require.NoError(t, err, "Could not process justification")

Expand Down
8 changes: 8 additions & 0 deletions testing/spectest/shared/deneb/rewards/rewards_penalties.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,16 @@ func runPrecomputeRewardsAndPenaltiesTest(t *testing.T, testFolderPath string) {

vp, bp, err := altair.InitializePrecomputeValidators(ctx, preBeaconState)
require.NoError(t, err)

vp, bp, err = altair.ProcessEpochParticipation(ctx, preBeaconState, bp, vp)
require.NoError(t, err)

activeBal, targetPrevious, targetCurrent, err := preBeaconState.UnrealizedCheckpointBalances()
require.NoError(t, err)
require.Equal(t, bp.ActiveCurrentEpoch, activeBal)
require.Equal(t, bp.CurrentEpochTargetAttested, targetCurrent)
require.Equal(t, bp.PrevEpochTargetAttested, targetPrevious)

deltas, err := altair.AttestationsDelta(preBeaconState, bp, vp)
require.NoError(t, err)

Expand Down

0 comments on commit 3d1d586

Please sign in to comment.