From f9e3b0a3c21a2d2ba3a298a4957184b15ae37c32 Mon Sep 17 00:00:00 2001 From: terencechain Date: Sat, 11 Jun 2022 08:54:33 -0700 Subject: [PATCH] Active balance: return `EFFECTIVE_BALANCE_INCREMENT` as min (#10866) --- .../core/helpers/rewards_penalties.go | 2 ++ .../core/helpers/rewards_penalties_test.go | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/beacon-chain/core/helpers/rewards_penalties.go b/beacon-chain/core/helpers/rewards_penalties.go index 25c63f1ff..1cae465b7 100644 --- a/beacon-chain/core/helpers/rewards_penalties.go +++ b/beacon-chain/core/helpers/rewards_penalties.go @@ -76,6 +76,8 @@ func TotalActiveBalance(s state.ReadOnlyBeaconState) (uint64, error) { return 0, err } + // Spec defines `EffectiveBalanceIncrement` as min to avoid divisions by zero. + total = mathutil.Max(params.BeaconConfig().EffectiveBalanceIncrement, total) if err := balanceCache.AddTotalEffectiveBalance(s, total); err != nil { return 0, err } diff --git a/beacon-chain/core/helpers/rewards_penalties_test.go b/beacon-chain/core/helpers/rewards_penalties_test.go index 4dd8b3c27..63ccfd1ab 100644 --- a/beacon-chain/core/helpers/rewards_penalties_test.go +++ b/beacon-chain/core/helpers/rewards_penalties_test.go @@ -74,6 +74,27 @@ func TestTotalActiveBalance(t *testing.T) { } } +func TestTotalActiveBal_ReturnMin(t *testing.T) { + tests := []struct { + vCount int + }{ + {1}, + {10}, + {10000}, + } + for _, test := range tests { + validators := make([]*ethpb.Validator, 0) + for i := 0; i < test.vCount; i++ { + validators = append(validators, ðpb.Validator{EffectiveBalance: 1, ExitEpoch: 1}) + } + state, err := v1.InitializeFromProto(ðpb.BeaconState{Validators: validators}) + require.NoError(t, err) + bal, err := TotalActiveBalance(state) + require.NoError(t, err) + require.Equal(t, params.BeaconConfig().EffectiveBalanceIncrement, bal) + } +} + func TestTotalActiveBalance_WithCache(t *testing.T) { tests := []struct { vCount int