diff --git a/beacon-chain/state/state-native/getters_participation_test.go b/beacon-chain/state/state-native/getters_participation_test.go index c30fdaa80b7..b5aac094ed3 100644 --- a/beacon-chain/state/state-native/getters_participation_test.go +++ b/beacon-chain/state/state-native/getters_participation_test.go @@ -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 @@ -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) } diff --git a/beacon-chain/state/stateutil/unrealized_justification.go b/beacon-chain/state/stateutil/unrealized_justification.go index e20f76a7bce..2a0181184fb 100644 --- a/beacon-chain/state/stateutil/unrealized_justification.go +++ b/beacon-chain/state/stateutil/unrealized_justification.go @@ -40,7 +40,7 @@ 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 { @@ -48,5 +48,20 @@ func UnrealizedCheckpointBalances(cp, pp []byte, validators []*ethpb.Validator, } } } + 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 +} diff --git a/beacon-chain/state/stateutil/unrealized_justification_test.go b/beacon-chain/state/stateutil/unrealized_justification_test.go index b5baa25d42e..f003e7951c6 100644 --- a/beacon-chain/state/stateutil/unrealized_justification_test.go +++ b/beacon-chain/state/stateutil/unrealized_justification_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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) }) diff --git a/testing/spectest/shared/altair/epoch_processing/justification_and_finalization.go b/testing/spectest/shared/altair/epoch_processing/justification_and_finalization.go index 2b7ad59efaa..75e4859b167 100644 --- a/testing/spectest/shared/altair/epoch_processing/justification_and_finalization.go +++ b/testing/spectest/shared/altair/epoch_processing/justification_and_finalization.go @@ -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") diff --git a/testing/spectest/shared/altair/rewards/rewards_penalties.go b/testing/spectest/shared/altair/rewards/rewards_penalties.go index ec4a7fca62d..3162f5f086d 100644 --- a/testing/spectest/shared/altair/rewards/rewards_penalties.go +++ b/testing/spectest/shared/altair/rewards/rewards_penalties.go @@ -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) diff --git a/testing/spectest/shared/bellatrix/epoch_processing/justification_and_finalization.go b/testing/spectest/shared/bellatrix/epoch_processing/justification_and_finalization.go index 8ec021a3860..36f8d6c0327 100644 --- a/testing/spectest/shared/bellatrix/epoch_processing/justification_and_finalization.go +++ b/testing/spectest/shared/bellatrix/epoch_processing/justification_and_finalization.go @@ -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") diff --git a/testing/spectest/shared/bellatrix/rewards/rewards_penalties.go b/testing/spectest/shared/bellatrix/rewards/rewards_penalties.go index 3ae1f613eb0..624d73b3f2a 100644 --- a/testing/spectest/shared/bellatrix/rewards/rewards_penalties.go +++ b/testing/spectest/shared/bellatrix/rewards/rewards_penalties.go @@ -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) diff --git a/testing/spectest/shared/capella/epoch_processing/justification_and_finalization.go b/testing/spectest/shared/capella/epoch_processing/justification_and_finalization.go index bbb5e5b78da..b818d5f34e1 100644 --- a/testing/spectest/shared/capella/epoch_processing/justification_and_finalization.go +++ b/testing/spectest/shared/capella/epoch_processing/justification_and_finalization.go @@ -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") diff --git a/testing/spectest/shared/capella/rewards/rewards_penalties.go b/testing/spectest/shared/capella/rewards/rewards_penalties.go index cdb60a47101..1651cd64630 100644 --- a/testing/spectest/shared/capella/rewards/rewards_penalties.go +++ b/testing/spectest/shared/capella/rewards/rewards_penalties.go @@ -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) diff --git a/testing/spectest/shared/deneb/epoch_processing/justification_and_finalization.go b/testing/spectest/shared/deneb/epoch_processing/justification_and_finalization.go index b6f9bb24348..574feddc5db 100644 --- a/testing/spectest/shared/deneb/epoch_processing/justification_and_finalization.go +++ b/testing/spectest/shared/deneb/epoch_processing/justification_and_finalization.go @@ -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") diff --git a/testing/spectest/shared/deneb/rewards/rewards_penalties.go b/testing/spectest/shared/deneb/rewards/rewards_penalties.go index abc8369dd0d..41bda166ce1 100644 --- a/testing/spectest/shared/deneb/rewards/rewards_penalties.go +++ b/testing/spectest/shared/deneb/rewards/rewards_penalties.go @@ -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)