diff --git a/beacon-chain/core/helpers/BUILD.bazel b/beacon-chain/core/helpers/BUILD.bazel index 00c9210f3..9ce9c70e1 100644 --- a/beacon-chain/core/helpers/BUILD.bazel +++ b/beacon-chain/core/helpers/BUILD.bazel @@ -63,6 +63,7 @@ go_test( embed = [":go_default_library"], shard_count = 2, deps = [ + "//beacon-chain/cache:go_default_library", "//beacon-chain/state:go_default_library", "//beacon-chain/state/stateutil:go_default_library", "//proto/beacon/p2p/v1:go_default_library", diff --git a/beacon-chain/core/helpers/validators.go b/beacon-chain/core/helpers/validators.go index 22923e613..783665b4e 100644 --- a/beacon-chain/core/helpers/validators.go +++ b/beacon-chain/core/helpers/validators.go @@ -110,7 +110,7 @@ func ActiveValidatorCount(state *stateTrie.BeaconState, epoch uint64) (uint64, e if err != nil { return 0, errors.Wrap(err, "could not interface with committee cache") } - if activeCount != 0 { + if activeCount != 0 && state.Slot() != 0 { return uint64(activeCount), nil } diff --git a/beacon-chain/core/helpers/validators_test.go b/beacon-chain/core/helpers/validators_test.go index 777c8d49b..19d9f918c 100644 --- a/beacon-chain/core/helpers/validators_test.go +++ b/beacon-chain/core/helpers/validators_test.go @@ -5,6 +5,7 @@ import ( "reflect" "testing" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/shared/hashutil" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -369,6 +370,40 @@ func TestDelayedActivationExitEpoch_OK(t *testing.T) { } } +func TestActiveValidatorCount_Genesis(t *testing.T) { + c := 1000 + validators := make([]*ethpb.Validator, c) + for i := 0; i < len(validators); i++ { + validators[i] = ðpb.Validator{ + ExitEpoch: params.BeaconConfig().FarFutureEpoch, + } + } + beaconState, err := beaconstate.InitializeFromProto(&pb.BeaconState{ + Slot: 0, + Validators: validators, + RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), + }) + if err != nil { + t.Fatal(err) + } + + // Preset cache to a bad count. + seed, err := Seed(beaconState, 0, params.BeaconConfig().DomainBeaconAttester) + if err != nil { + t.Fatal(err) + } + if err := committeeCache.AddCommitteeShuffledList(&cache.Committees{Seed: seed, ShuffledIndices: []uint64{1, 2, 3}}); err != nil { + t.Fatal(err) + } + validatorCount, err := ActiveValidatorCount(beaconState, CurrentEpoch(beaconState)) + if err != nil { + t.Fatal(err) + } + if validatorCount != uint64(c) { + t.Error("Did not get the correct validator count") + } +} + func TestChurnLimit_OK(t *testing.T) { tests := []struct { validatorCount int